nitation Posted August 22, 2008 Share Posted August 22, 2008 Hello, I have populated record from my database to a table. This is my question, How do i have a checkbox for every record retrieved from my database and each checkbox should represent each record respectively. To break it down: i have this in a table: ID RecordName Recordcheckbox ----------------------------------------------- 1 Business how do i add it (the value of this checkbox should be "Business") 2 System how do i add it (the value of this checkbox should be "System") 3 School how to i add it. (the value of this checkbox should be "School") I hope my question is straight. Regards Link to comment https://forums.phpfreaks.com/topic/120862-solved-variable-assigned-to-a-checkbox/ Share on other sites More sharing options...
BlueSkyIS Posted August 22, 2008 Share Posted August 22, 2008 i typically append the record id to the end of the checkbox NAME, for instance: $sql = "SELECT ID, RecordName FROM yourTable ORDER BY ID"; $result = mysql_query($sql) or die(mysql_error()); while (list($rec_id, $rec_name) = mysql_fetch_row($result)) { echo "<INPUT TYPE='checkbox' NAME='checkbox$rec_id' VALUE='1'> $rec_name<BR>"; } on submit, i'd do a similar thing, looping over id's to see if checkbox(id) is checked. Link to comment https://forums.phpfreaks.com/topic/120862-solved-variable-assigned-to-a-checkbox/#findComment-623032 Share on other sites More sharing options...
BlueSkyIS Posted August 22, 2008 Share Posted August 22, 2008 reading my solution, i realize there is probably a simpler solution: use the same checkbox name, but set the VALUE to the ID of each record: $sql = "SELECT ID, RecordName FROM yourTable ORDER BY ID"; $result = mysql_query($sql) or die(mysql_error()); while (list($rec_id, $rec_name) = mysql_fetch_row($result)) { echo "<INPUT TYPE='checkbox' NAME='selectedRecords' VALUE='$rec_id'> $rec_name<BR>"; } then loop over the $_POST['selectedRecords'] array to get each selected ID. (make sure $_POST['selectedRecords'] is an array before trying foreach). Link to comment https://forums.phpfreaks.com/topic/120862-solved-variable-assigned-to-a-checkbox/#findComment-623034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.