Page 1 of 1

[SOLVED] Loop problems

Posted: Thu Sep 30, 2004 9:19 pm
by SBro
I have the following bit of code (which will eventually be used to construct some queries, but for the moment is just testing the correct data is being passed.

Everything seems to be working fine, however (currently 2 records being passed to the page with the code below) the last status doesn't get displayed. The $num gets displayed for each record, and the correct status for the first record gets displayed but not the 2nd one. Which is strange because if i substitute $status[$x] with $status[2] the 2nd record gets displayed, any help would be appreciated, thankyou.

Code: Select all

<?php
$num = $_POST['number'];
for ($x = 0; $x < count($num); $x++) {
	echo 'Num: '.$num[$x].'<br>';
	$status = $_POST['status'.$num[$x].''];
	echo 'Status: '.$status[$x].'<br>';
}
?>

Posted: Thu Sep 30, 2004 9:53 pm
by nigma
Whats the variable 'num' have as its value? I'm thinking that maybe the loop ends before x gets to be 2

Posted: Thu Sep 30, 2004 10:14 pm
by SBro
The variable num is an array which has any number of elements, at the moment it has 2...I don't think it can be that the loop is ending, because when the loop gets initiated for the 2nd time, Num: $num prints out fine but the Status doesn't, wierd.

Posted: Thu Sep 30, 2004 10:25 pm
by nigma
Do you even get "Status: "? And you say that if you hardcode 2 into the brackets you get the wanted result?

All I can say is maybe try echoing the value of $x after you set the variable $status to make sure it has the right value. And maybe try using array_map. Sorry I can't be of much more help.

Posted: Thu Sep 30, 2004 10:31 pm
by SBro
Silly me, I changed the code to that below...i changed the 2nd last line, I changed the [$x] to [0] and it works, because there is only one element in that array and thus doesn't need the $x, thanks for your help :D

Code: Select all

<?php
$num = $_POST['number'];
for ($x = 0; $x < count($num); $x++) {
	echo 'Num: '.$num[$x].'<br>';
	$status = $_POST['status'.$num[$x].''];
	echo 'Status: '.$status[0].'<br>';
}

?>