Forum Moderators: coopster

Message Too Old, No Replies

Checkboxes values array

         

Gian04

9:19 am on Nov 1, 2008 (gmt 0)

10+ Year Member



I have a code on my page to display records from DB


$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?

Gian04

12:59 am on Nov 2, 2008 (gmt 0)

10+ Year Member



Anyone?, please help. Im stucked with this.

jatar_k

12:52 pm on Nov 2, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the first thing to do is

echo 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.

malcolmcroucher

2:00 pm on Nov 2, 2008 (gmt 0)

10+ Year Member



i think this works

<?php

$sql1="SELECT * FROM country ORDER BY country";

$query1 = mysql_query($sql1);

echo '<select name="users" ;

while

($row1 = mysql_fetch_array($query1))

{

$i;

echo '<option value="',$row1['country'],'">',$row1['country'] ,'</option>';

$i++ ;

}

echo '</select>';

?>

Gian04

12:59 am on Nov 3, 2008 (gmt 0)

10+ Year Member



Hi jatar_k,

the first thing to do is

echo 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.

Anyango

5:55 am on Nov 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




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)
{
//..........
}