pocobueno1388 Posted May 1, 2008 Share Posted May 1, 2008 I am in the process of programming a page where an admin can give/delete powers that other moderators have on the site (screen shot attached). I have no problem updating the DB when a checkbox IS checked, the problem is I can't figure out a way to know which checkboxes were left unchecked, so I can update the DB to take away that power. Currently this is how my checkbox HTML looks: <input type='checkbox' name='power[chat][]' value='{$row['userID']}'> The name attribute varies depending on what the checkbox is for. So when I submit the form, the array looks like this giving me the CHECKED values: Array ( [forum_addDelCategory] => Array ( [0] => 1 [1] => 3 ) [chat] => Array ( [0] => 1 [1] => 3 ) [news] => Array ( [0] => 3 ) [forum_moderate] => Array ( [0] => 3 ) ) I have a DB table called moderators (screen shot attached). It holds the mods userID and lists all the powers they can have. If they have the power to do something, there is a 1 in the field. If they don't, there is a 0. Any advice is greatly appreciated, thanks. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/103752-solved-figuring-out-which-checkboxes-were-not-checked-to-update-db/ Share on other sites More sharing options...
benphp Posted May 1, 2008 Share Posted May 1, 2008 Why not loop the results and do if(checkbox != "0") { inside the loop? Quote Link to comment https://forums.phpfreaks.com/topic/103752-solved-figuring-out-which-checkboxes-were-not-checked-to-update-db/#findComment-531203 Share on other sites More sharing options...
GingerRobot Posted May 1, 2008 Share Posted May 1, 2008 How's about cycling through each moderator and checking the relevant array for their priviledges: <?php $array = array('forum_addDelCategory'=>array(1,3),'chat'=>array(1,3),'news'=>array(3),'forum_moderate'=>array(3)); $moderators = array(1,3);//list of moderators pulled from the table foreach($moderators as $v){ $update = ''; foreach($array as $k2=>$v2){ if(in_array($v,$v2)){ $update.=$k2.'=1 '; }else{ $update.=$k2.'=0 '; } } $sql = "UPDATE tbl SET $update WHERE userID=$v"; mysql_query($sql) or die(mysql_error()); } ?> The above is tested and works. You'll need to grab the array of moderators from the table first. You'll also need to carefully check the user input, since it's relying on the form's names and values in the query. Quote Link to comment https://forums.phpfreaks.com/topic/103752-solved-figuring-out-which-checkboxes-were-not-checked-to-update-db/#findComment-531231 Share on other sites More sharing options...
pocobueno1388 Posted May 2, 2008 Author Share Posted May 2, 2008 Thanks GingerRobot, that gave me a good start, but it's still having one issue. If you uncheck all the checkboxes from a column, that columns field isn't added to the array...therefore it completely ignores anything you did to that column and just keeps all the original values how they were. I don't know why I'm having such a difficult time with this array. I need to figure out a way to keep all values in the array even if no checkboxes are checked. Any suggestions for that? Here is the code I'm using: <?php if (isset($_POST['update'])){ //echo '<pre>',print_r($_POST['power']),'</pre>'; $get_mods = mysql_query("SELECT userID FROM moderators"); while ($row = mysql_fetch_assoc($get_mods)){ $update = array(); foreach ($_POST['power'] as $field => $userID){ if(in_array($row['userID'], $userID)) $update[] = $field.'=1'; else $update[] = $field.'=0'; } $sql = "UPDATE moderators SET " . implode(", ", $update) . " WHERE userID={$row['userID']}"; mysql_query($sql) or die(mysql_error()."<p>$sql<p>"); echo $sql."<p>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103752-solved-figuring-out-which-checkboxes-were-not-checked-to-update-db/#findComment-532108 Share on other sites More sharing options...
sasa Posted May 3, 2008 Share Posted May 3, 2008 try to change your HTML to <input type="hidden" name='power[chat][{$row['userID']}]' value='0'> <input type='checkbox' name='power[chat][{$row['userID']}]' value='1'> Quote Link to comment https://forums.phpfreaks.com/topic/103752-solved-figuring-out-which-checkboxes-were-not-checked-to-update-db/#findComment-532251 Share on other sites More sharing options...
pocobueno1388 Posted May 3, 2008 Author Share Posted May 3, 2008 Thanks, but I fixed it. Here is the final code: <?php if (isset($_POST['update'])){ //make sure all powers are in array $powers = array("news", "forum_addDelCategory", "forum_moderate", "chat"); foreach ($powers as $pow){ if (!isset($_POST['power'][$pow])) $_POST['power'][$pow] = NULL; } $get_mods = mysql_query("SELECT userID FROM moderators"); while ($row = mysql_fetch_assoc($get_mods)){ $update = array(); foreach ($_POST['power'] as $field => $userID){ if(@in_array($row['userID'], $userID)) $update[] = $field.'=1'; else $update[] = $field.'=0'; } $sql = "UPDATE moderators SET " . implode(", ", $update) . " WHERE userID={$row['userID']}"; mysql_query($sql) or die(mysql_error()."<p>$sql<p>"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103752-solved-figuring-out-which-checkboxes-were-not-checked-to-update-db/#findComment-532449 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.