Page 1 of 1

[SOLVED] formula loop problem

Posted: Sat Mar 26, 2005 2:21 pm
by bwalk54
I'm a newbie to coding so I hope this is not an incredibly stupid question. I can't figure out why this loop isn't working.

Code: Select all

<?php
for ($i=0; $i < count($rarea); $i++)	{
	if ($rarea[$i] != "")	{
		echo $rarea[$i] . "<br>";
		$_SESSION["res_" . $rarea[$i]] = $_POST["res_" . $rarea[$i]];
		$res_spec = $_SESSION["res_" . $rarea[$i]];
		for ($i=0; $i < count($res_spec); $i++)	{	
			echo $res_spec[$i] . "<br>";
		}
	}
}
?>
$rarea is obviously an array (ie. bis, cbb, frr, ...)
I have a form which creates and populates additional arrays of selections based on values in $rarea, each one begins with "res_", for example "res_bis" would be one of the array names, or res_cbb,....
The loop runs once successfully, but does not move onto the next value of $rarea. I ran a loop to see if the other $rarea values were being passed:

Code: Select all

<?php
for ($i=0; $i < count($rarea); $i++)	{
	echo $rarea[$i] . "<br>";
	}?>
and got a good/correct response, so what is wrong with the first for loop?

Any suggestions would be appreciated.

PS I apologize in advance if this code was not posted correctly. If so, please let me know what I did wrong.

Posted: Sat Mar 26, 2005 2:23 pm
by John Cartwright
What is being outputted? Errors?

Posted: Sat Mar 26, 2005 2:28 pm
by bwalk54
The out put for the first 'for' loop (the one in question):
bis ($rarea[0])
selection1 (res_spec[0])
selection2 (res_spec[1])
selection3 (res_spec[2])

-- ($rarea[1])
Here in lies the problem, it won't move to the next value of $rarea.

For second 'for' loop:
bis ($rarea[0])
cbb ($rarea[1])
sn ($rarea[2])

(Correct response.)

No error output.

Posted: Sat Mar 26, 2005 2:30 pm
by John Cartwright

Code: Select all

<?php
for ($i=0; $i < count($rarea); $i++)	{
	if ($rarea[$i] != "")	{
		echo $rarea[$i] . "<br>";
		$_SESSION["res_" . $rarea[$i]] = $_POST["res_" . $rarea[$i]];
		$res_spec = $_SESSION["res_" . $rarea[$i]];
		for ($i=0; $i < count($res_spec); $i++)	{	
			echo $res_spec[$i] . "<br>";
		}
	}
}
?>
Seems you are using $i for both loops. The first $i is being overwritten.

Posted: Sat Mar 26, 2005 2:34 pm
by bwalk54
I knew it was something obvious, sorry to waste your time, THANKS!

Posted: Sat Mar 26, 2005 2:36 pm
by John Cartwright
No probem, enjoy.