Forum Moderators: coopster

Message Too Old, No Replies

checkboxes and sql row results

         

sphiro

5:53 am on Apr 7, 2005 (gmt 0)

10+ Year Member



I am trying to place a checkbox beside each row that is inputed into the database.

Now this is how I diplay the rows:

print "<br><br>There are $num_rows records.<P>";
print "<table width=500 border=0>\n";
while ($get_info = mysql_fetch_row($result)) {
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";

How would I go about attaching this:


<input type="checkbox" value="..." name="...">

to the side of each displayed row? Also, how would I assign the row id to the checkbox so that once the box is check and the action initiated the row would be deleted?

ironik

10:19 pm on Apr 7, 2005 (gmt 0)

10+ Year Member



You could add your checkbox before you loop your fields as an extra <td>


print "<br><br>There are $num_rows records.<P>";
print "<table width=500 border=0>\n";
while ($get_info = mysql_fetch_row($result)) {
print "<tr>\n";
print "<td><input type=\"checkbox[]\" value=\"" . $get_info['row_id'] . "\" name=\"check_" . $get_info['row_id'] . "\"></td>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";

And your php to access the checkbox's value would be:


if (isset($_POST['checkbox']))
{
foreach ($_POST['checkbox'] as $checkBoxName=>$checkBoxValue)
{
echo 'The database id: ' . $checkBoxValue . ' has been checked';
}
}

sphiro

4:45 am on Apr 9, 2005 (gmt 0)

10+ Year Member



Could you clarify this a bit?
"<td><input type=\"checkbox[]\" value=\"" . $get_info['row_id'] . "\" name=\"check_" . $get_info['row_id'] . "\"></td>\n"

I tried it, but it displays text fields instead of checkboxes.

sphiro

6:12 am on Apr 9, 2005 (gmt 0)

10+ Year Member



Nevermind, I got it.

I want to be able to delte the item which is 'checked', so this is how I am attempting to go about it:

$num_rows = mysql_num_rows($result);
print "<br><br>There are $num_rows records.<P>";
print "<table width=200 border=0>\n";
while ($get_info = mysql_fetch_row($result)) {
print "<tr>\n";
print "<td><input type=\"checkbox\" value=\"" . $get_info['idtable'] . "\" name=\"check_" . $get_info['idtable'] . "\"></td>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
echo "<input name=Submit type=submit>";

With this being the delete code:


if(isset($_POST['submit'])){
$sql = "DELETE FROM items";
$result = mysql_query($sql);
echo "Record deleted!";
}

I think I am missing something, but I am not sure what exactly. Any suggestions?

mcibor

2:02 pm on Apr 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ironic's way is nitter, however he mixed the code a bit. It should be:


print "<td><input type=\"checkbox\" value=\"" . $get_info['row_id'] . "\" name=\"check[]\"></td>\n";

This way there's an array of checked checkboxes, and you don't have to loop through all of them.

Best wishes
Michal Cibor

BTW. Welcome to webmasterworld!

To delete:
$ids = implode(',', $_POST["check"]); // I'm not sure if this'll do the trick
DELETE FROM items WHERE id IN ($ids); // IN (1,3,6) will erase rows with id = 1, 3 and 6

sphiro

4:09 pm on Apr 9, 2005 (gmt 0)

10+ Year Member



I have revised the code accordingly, however, I am not sure how I can attach the delete code that you posted to button such that when clicked it will delete the checked checkboxes.

mcibor

6:37 pm on Apr 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The checkboxes:
$num_rows = mysql_num_rows($result);
print "<br><br>There are $num_rows records.<P>";
print "<table width=200 border=0>\n";
while ($get_info = mysql_fetch_row($result)) {
print "<td><input type=\"checkbox\" value=\"" . $get_info['row_id'] . "\" name=\"check[]\"></td>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
echo "<input name=Submit type=submit>";
(change is bold)

delete code:

if(isset($_POST['submit'])){
$ids = implode(',', $_POST["check"]);
$sql = "DELETE FROM items WHERE id IN ($ids)";

if($result = mysql_query($sql)) echo "Records deleted!"; //there are as many records deleted as there are checkboxes checked
}

Hope this helps!
Michal Cibor

sphiro

8:03 pm on Apr 9, 2005 (gmt 0)

10+ Year Member



This is very helpful information. Thanks for replying with the direct modifications, but I seem to have a problem.
if(isset($_POST['submit'])){
$ids = implode(',', $_POST["check"]);
$sql = "DELETE FROM items WHERE id IN ($ids)";
if($result = mysql_query($sql)) echo "Records deleted!"; //there are as many records deleted as there are checkboxes checked
}

I assume that the bold 'id' above should be the same as the bold 'row_id' below?
print "<td><input type=\"checkbox\" value=\"" . $get_info['row_id'] . "\" name=\"check[]\"></td>\n";

I tried it the way that you posted it, then I tried it with 'id' being changed to 'row_id', however it doesn't seem to be going. I mean, it doesn't seem that the action is being instantained once the button is clicked.

mcibor

8:48 pm on Apr 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it should be row_id, sorry, didn't noticed that. I tried the below code:

<body><form action="#" method="POST">
<?php
for($i = 1; $i <= 10; $i++)
{
echo $i.". "?>
<input type="checkbox" value="<?php echo $i;?>" name="check[]">
<?
}
$ids = implode(',', $_POST["check"]);
print "<p>Ids: $ids</p>";
$ask = "UPDATE items SET idtable='8' WHERE row_id IN ($ids)";//just exemplary
if(my_query($ask)) echo "Done"; //function my_query first connects to a db, then asks a query
else echo "Not good:(";?>
<input type="submit">
</form>
</body>

The problem may be, that you don't connect to the db for the second time. I'm not sure how it is done, but that could be it (the php forgets what to connect).

Michal Cibor

sphiro

9:25 pm on Apr 9, 2005 (gmt 0)

10+ Year Member



Thanks for the help!