Forum Moderators: coopster
$cars = $_POST['cars'];
...
while($my_row = mysql_fetch_row($list_results)) {
<input type='checkbox' name='cars[]' value='<?=$my_row[1]?>' /> <?=$my_row[3]?>
} Im sure the table have 50 records and the code above displays it on my page without any problem
But my problem is I cant retrieve the contents of the array:
for ($i=0; $i<count($cars); $i++) {
echo $cars[$i];
} Will display "A"
echo $cars[0]; //will display "A"
echo $cars[1]; //will display "r"
echo $cars[2]; //will display "r"
echo $cars[3]; //will display "a"
echo $cars[4]; //will display "y"
Whats the problem with my code?
the first thing to do isecho count($cars);
and
print_r($cars);to see what is in your array. If there is only a single row then you go back to your array creation and you actual table in mysql.
Yes Im only getting a single row, and that is actually my problem, but Im sure that the table have 50 records and my "while looping" displays all this records correcly on my page.
Yes Im only getting a single row, and that is actually my problem, but Im sure that the table have 50 records and my "while looping" displays all this records correcly on my page.
Give it an Index. Yes i know PHP arrays work even when you keep adding values without index but try giving it an index, it works for me with index.
like this
$i=0;
while($my_row = mysql_fetch_row($list_results))
{
echo "<input type='checkbox' name='cars[$i]' value='".$my_row[1]."' />".$my_row[3];
$i++;
}
and then on the next page
foreach ($_POST[cars] as $key => $value)
{
//..........
}