Jump to content

Recommended Posts

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]

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.

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>";
      
   }
   
}

?>

 

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>");
      
   }

}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.