Forum Moderators: coopster

Message Too Old, No Replies

Checkbox array issues

PHP, Checkbox, Array

         

Esqulax

12:21 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



Hiya,
Same story as most, im really new to PHP (about 3 weeks), and self learning. i've hit a wee hitch, and wondering if one of you chaps, could help me out!

Basically, i have a system that writes to a MySQL Database, i have a system to view the records. im having trouble deleting multiple records.

Ok, i made a loop with all the checkboxes named 'guestid[]' to create the array from the form.

I then created an if statement, with a for loop, to display the records i selected (Deleting should be straightforward, i made this so i know what values are being passed into the array)

Ok, the Count, correctly identifies the amount of records selected.... great
the for loop displays the correct number of array instances.. great-ish
The problem is, the array elemnt dont have the information that i want in them (being the guest_id data from the sql database)

The .$row['guest_id'] part in the table works, and displays the correct value


<form method="post" action="<?php echo($self);?>">
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<tr>\n";
echo "<td>". $row['guest_id']?><input name="guestid[]" type="checkbox" value=<?php $row['guest_id']?>></td>
<?php
echo "<td>" . $row['name'] ;
echo "<td>" . $row['surname'];
}


?>
<tr>
<td>

<input type="submit" name="submit" value="submit"></td>
</form>
</tr>
</table>

<?php


$delid = $_POST["guestid"];
$how_many=count($delid);

echo("".$how_many."<br>");
if ($how_many>0)
{
echo ("You chose the following records:<br>");
}
for ($i=0; $i<$how_many; $i++)
{
echo (($i+1) . " - " . $delid[$i] . "<br>");
}

?>

(Its messy, but the this is the way i program... make snippets that work, tidy up, then put into the main code)

Thanks in advance

jatar_k

1:43 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Esqulax,

this portion seems to have some mismatched tags

echo "<tr>\n";
echo "<td>". $row['guest_id']?><input name="guestid[]" type="checkbox" value=<?php $row['guest_id']?>></td>
<?php
echo "<td>" . $row['name'] ;
echo "<td>" . $row['surname'];

try this replacement

echo "<tr>\n";
echo "<td>". $row['guest_id'] . '<input name="guestid[]" type="checkbox" value="' . $row['guest_id'] . '"></td>';
echo "<td>" . $row['name'] ;
echo "<td>" . $row['surname'];

see if that makes the difference.

Esqulax

2:41 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



Aha!
Spot on!

My problem, mainly, was with the echo function, as when putting html into it, a \ has to precede every " So i was kinda stopping the PHP, HTML-ing it, then restarting the PHP..

Didn't think of using ' as the open and closer for echo!

Thanks heaps!