[SOLVED] formula loop problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bwalk54
Forum Newbie
Posts: 3
Joined: Sat Mar 26, 2005 1:55 pm
Location: MD

[SOLVED] formula loop problem

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What is being outputted? Errors?
bwalk54
Forum Newbie
Posts: 3
Joined: Sat Mar 26, 2005 1:55 pm
Location: MD

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
bwalk54
Forum Newbie
Posts: 3
Joined: Sat Mar 26, 2005 1:55 pm
Location: MD

Post by bwalk54 »

I knew it was something obvious, sorry to waste your time, THANKS!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

No probem, enjoy.
Post Reply