Forum Moderators: coopster

Message Too Old, No Replies

Looping through checkboxes to update a database

checkbox loop database

         

nailonthehead

6:48 am on Jun 1, 2007 (gmt 0)

10+ Year Member



I'm developing an application that is highly database dependent. I've to use checkboxes in a number of places so as to ease the use of the system. However, I'm experiencing problems when it comes to looping through the checkboxes so as to update the related info to the database.

I'm retrieving info from a a database, then the user has to select a number of unpredetermined records. When he does this, a flag for the selected record is set. But I experience problems when it comes to looping through the checkboxes to set the flags for the selected records.

Could someone please come to my rescue.
Thanks in advance.

phparion

7:26 am on Jun 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



let me divide this into steps for your ease of understanding

1 - create an ARRAY OF CHECKBOXES and their values should be a unique value from the table which is unique for each record e.g primary key

2 - on the action page retrieve the Array of CheckBoxes in a foreach loop and perform queries

a dirty example could be

echo "<input type=checkbox name=cbox[] value=".$row['item_id'].">";
echo "<input type=checkbox name=cbox[] value=".$row['item_id'].">";
echo "<input type=checkbox name=cbox[] value=".$row['item_id'].">";
echo "<input type=checkbox name=cbox[] value=".$row['item_id'].">";

on the action page

if(sizeof($_POST['cbox'])) {

//means if at least one check box is selected

foreach($_POST['cbox'] AS $id) {

mysql_query("UPDATE tablename SET ... WHERE item_id=$id");
} //end foreach

} //end IF