Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sigh.... creating different variables in FOR EACH loop

Status
Not open for further replies.

Quasibobo

Programmer
Oct 11, 2001
168
NL
Ok.... it's me again (huge PHP-fan and ASP-newbie)

With ASPsmartUpload I can upload multiple files. Now I'd like to seperate these files one by one. In php it would be something like this:

Code:
$i=1
FOREACH (file AS myupload.Files)
  {
  myFile.$i=file.FileName
  $i++
  }
so the files get (for example)
myFile1="PicOne.jpg"
myFile2="PicTwo.jpg"
myFile3="PicThree.jpg"
etc. etc.

How can I acchieve this in ASP? If there's a way with a FOR i=0 to 2 LOOP, that would be fine too (3-files is de maximum upload)

I was thinking something like
Code:
picInt=0

For each file In myupload.Files
  myFile+picInt=file.FileName(picInt)
  picInt=picInt+1
Next

Response.Write (myFile0 & " and " & myFile1& " and " & myFile2 & " are uploaded")

Any suggestions...? I'm getting out of idea's and samples for ASPsmartUpload don't help me at all...

Quasibobo

Don't eat yellow snow!
 
You don't need picInt - For Each works quite similar as PHP foreach( $collection as $item ):
Code:
fileList = ""
For Each file In myupload.Files
	If fileList <> "" Then fileList = fileList & " and "
	fileList = fileList & file.FileName
Next

Response.Write( fileList & " are uploaded" )
 
Er...I think the point was to place the files in variables that were created in order. you can do that in ASP, but I'm not sure what kind of efficiency hit you would be looking at. It might be better to use an array or use only one variable, do whatever you need to with the file, then move on to the next.
The biggest issue I see with using dynamically named variables is that I don't see how you plan on accessing them later. You can't loop through them or anything like that, I gues you would reuse the i counter in another loop. If thats the case then:
Code:
Dim i
i = 1
For Each file in myUpload.Files
   'The execute method takes a string and executes it as a line instruction
   Execute("myfile" & i & " = file")
   i = i + 1
Next

I would still prefer an array though, gives you more flexibility and better efficiency:
Code:
Dim i, arrFiles()
i = 0
'resize array to hold COunt elements
ReDim arrFiles(myUpload.Files.Count - 1)
For Each file in myUpload.Files
   arrFiles(i) = file
   i = i + 1
Next

Hope this helps,
-T


01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Thanks Tarwn... I'll have a go with that!

Quasibobo

Don't eat yellow snow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top