Closed Thread Icon

Preserved Topic: FOR loop in PHP Pages that link to <a href="https://ozoneasylum.com/backlink?for=12043" title="Pages that link to Preserved Topic: FOR loop in PHP" rel="nofollow" >Preserved Topic: FOR loop in PHP\

 
Author Thread
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-08-2002 18:49

Hmmm...seems my loop isn't loopy enough...

I have a form, that has two name fields ($fname[1] and $fname[2]), and related email fields ($femail[1] and $femail[2]). The form then submits back to the same page, where a loop should kick off some stuff. Here's the code:

$items = 2;
$counter = 0;
for ($numitems=0; $numitems < $items; $numitems++) {
if (($fname[$numitems]!="") && ($femail[$numitems]!="")) {
// do some stuff;
$counter++;
$tally.="$fname[$numitems], ";
}
}
print("Counter: $counter<br />\n");
print("Tally: $tally<br />\n");


However, the loop only cycles once, with the data for [1]. I have verified that the $fname[2] and $femail[2] actually reach the script, but $counter only ends up being "1", and $tally only contains the value for $fname[1].

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-08-2002 18:53

Fixed -

I changed
($numitems=0; $numitems < $items; $numitems++)
to
($numitems=0; $numitems <= $items; $numitems++)

and it works.

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 02-08-2002 18:54

set $items=3;
then it will also catch the [2] arrays...

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 02-08-2002 18:56

Pugz, I'm a little unclear what is happening here however a quick guess?

$numitems starts at 0 and will only count until 1 (< $items) so it's getting $fname[0] and $fname[1]. and you have them named [1] and [2].

Can some one switch forums on this?



:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 02-08-2002 18:57

LOL -can we all post at once or what!




:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-08-2002 18:59

Ooops - didn't realize I posted in the wrong forum.

I need a vacation!

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 02-08-2002 19:45

I'll move it to the right board in a sec...

[edit]
Done
[/edit]




[This message has been edited by mr.maX (edited 02-08-2002).]

« BackwardsOnwards »

Show Forum Drop Down Menu