luxman Posted July 31, 2007 Share Posted July 31, 2007 Hello freaks I have dynamic checkboxes with values populated from a database table and want to set the boxes to selected based on values from another table. Basically, I have a table of users, a table of categories (populates the checkboxes) and a table that is populated when multiple categories are selected per user (the table used to set the checkboxes to selected)..hope this is clear..Its for an 'Edit user' page, displaying the info for the user. Is this possible? This what I currently have: <?php do { ?> <input <?php if (!(strcmp($row_userCats['dlcatID'],$row_engineerCat['dlCatID'] ))) {echo "checked=\"checked\"";} ?> type="checkbox" name="checkbox[]" value="<?php echo $row_engineerCat['dlCatID']; ?>" /> <span class="formLBL"><?php echo $row_engineerCat['dlCat']; ?></span> </label> <br /> <?php } while ($row_engineerCat = mysql_fetch_assoc($engineerCat)); ?> (yes it is doctored dreamweaver code ) But this only selects the first returned value from the userCats recordset. Any help would be greatly appreciated. Mitch Quote Link to comment https://forums.phpfreaks.com/topic/62647-selecting-multiple-dynamic-checkboxes-from-a-database/ Share on other sites More sharing options...
hitman6003 Posted August 1, 2007 Share Posted August 1, 2007 This should get you started. Table structure for my example; categories table: cat_id cat_name users table: user_id user_name user_categories table: cat_id user_id This will display all the users and categories as checkboxes, if the user already has that category, it's already checked... $query = "SELECT * FROM categories"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $cats[$row['cat_id']] = $row['cat_name']; } $query = "SELECT user_name, cat_name " . "FROM user_categories uc " . " LEFT JOIN users u ON u.user_id = uc.user_id " . " LEFT JOIN categories c ON c.cat_id = uc.cat_id " . "ORDER BY user_name, cat_name"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $user_data[$row['user_name']][] = $row['cat_name']; } echo ' <form method="post"> <table>'; foreach ($user_data as $user => $user_cats) { echo ' <tr> <th colspan="' . count($cats) . '">' . $user . '</th> </tr> <tr>'; foreach ($cats as $cat) { echo ' <td> <input type="checkbox" name="user_cats[' . $user . '][]" value="' . $cat . '" ' . (in_array($cat, $user_cats) ? 'checked="checked"' : '') . '> </td>'; } echo ' </tr>'; } echo ' <tr> <td colspan="' . count($cats) . '"><input type="submit" name="submit" value="Save"></td> </tr> </table> </form>'; When the form is posted it will create a multidimensional array that can be looped through and the data saved to the database...in this example, since all users are displayed and updated at the same time, you could truncate the user_categories table, then just do inserts. Alternatively, you'd have to check each users categories, then do updates / deletes as necessary. Do a print_r on $_POST to see what it looks like, which should help with how to do the looping. Quote Link to comment https://forums.phpfreaks.com/topic/62647-selecting-multiple-dynamic-checkboxes-from-a-database/#findComment-312420 Share on other sites More sharing options...
luxman Posted August 1, 2007 Author Share Posted August 1, 2007 Fantastic Hitman I'll give that whirl and see how I go.. many thnx Quote Link to comment https://forums.phpfreaks.com/topic/62647-selecting-multiple-dynamic-checkboxes-from-a-database/#findComment-312574 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.