[SOLVED] Loop problems

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
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

[SOLVED] Loop problems

Post 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>';
}
?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Whats the variable 'num' have as its value? I'm thinking that maybe the loop ends before x gets to be 2
Last edited by nigma on Mon Oct 04, 2004 10:09 am, edited 1 time in total.
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post 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.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post 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>';
}

?>
Post Reply