Hi

I am struggling how to list all the permissions and with checkboxes.

if anyone can help me with it this is the code

            echo "<table cellpadding='0' cellspacing='0' width='100%' class='table table-bordered'>";
                echo "<tr>";
                    echo "<th>PERMISSION</th>";
                    foreach($user_groups->rows as $row) {
                        echo "<th class='checkbox'>".$row['title']."</th>";
                    }
                echo "</tr>";

                $users_permission_query = $this->db->query("SELECT * FROM users_permission");

                foreach($users_permission_query->rows as $row) {
                    $group_permissions_rows = json_decode($row['permission'], TRUE);
                    echo "<tr>";
                    echo "<td>".$row['module']."<br />";
                        foreach($group_permissions_rows as $key => $value) {
                            echo "{$key}<br>";
                        }
                    echo "</td>";
                    echo "<td>";
                        echo "<table cellpadding='0' cellspacing='0' width='100%'>";
                        for($i=0; $i < count($group_permissions_rows); $i++) {
                            foreach($group_permissions_rows as $key => $value) {
                                echo "<tr>";
                                echo "<td><input type='checkbox' name='permissions[" . $key . "]'";
                                if($value) {
                                    echo ($value==1 ? " checked =\"checked\"" : ""); 
                                }
                                echo " value=$value /></td>";
                                echo "</tr>";
                            }
                        }
                        echo "</table>";
                    echo "</td>";
                    echo "</tr>";
                }

            echo "</table>";

Dani AI

Generated

your table renders duplicates because you loop twice over the same permission list and you also set each checkbox value to the current DB value instead of a fixed value (1). Unchecked boxes submit nothing, so you must either add a hidden 0 for each field or normalize on the server. Also decide whether you are editing one group at a time or all groups side by side. From your header with group titles, it looks like a matrix: rows are module.action, columns are groups.

Render (matrix: module.action x group):

echo '<table class="table table-bordered">';
echo '<tr><th>Permission</th>';
foreach ($user_groups->rows as $g) {
    echo '<th>'.htmlspecialchars($g['title'], ENT_QUOTES).'</th>';
}
echo '</tr>';

foreach ($table_permissions as $module => $actions) {
    foreach ($actions as $action => $default) {
        echo '<tr>';
        echo '<td>'.htmlspecialchars($module).' : '.htmlspecialchars($action).'</td>';
        foreach ($user_groups->rows as $g) {
            $gid = (int)$g['id'];
            $checked = !empty($permByGroup[$gid][$module][$action]); // from DB json_decode
            // hidden 0 ensures a value when unchecked
            echo '<td><input type="hidden" name="permissions['.$gid.']['.$module.']['.$action.']" value="0">';
            echo '<input type="checkbox" value="1" name="permissions['.$gid.']['.$module.']['.$action.']" '.($checked?'checked':'').'></td>';
        }
        echo '</tr>';
    }
}
echo '</table>';

Save (one JSON blob per group_id):

if ($_SERVER['REQUEST_METHOD']==='POST') {
    $in = $_POST['permissions'] ?? [];
    foreach ($in as $gid => $mods) {
        $clean = [];
        foreach ($table_permissions as $module => $actions) {
            foreach (array_keys($actions) as $action) {
                $clean[$module][$action] = isset($mods[$module][$action]) && $mods[$module][$action]=='1' ? 1 : 0;
            }
        }
        $json = json_encode($clean, JSON_UNESCAPED_SLASHES);
        $stmt = $db->prepare('UPDATE users_permission SET permission=? WHERE group_id=?');
        $stmt->execute([$json, (int)$gid]);
    }
}

Notes:

  • Remove the inner for(count(...)) loop; one foreach is enough.
  • Escape all output; validate modules/actions against an allowed list.
  • Use prepared statements and a CSRF token.

and are right to ask for clarity: specify whether you store one row per group or per module, then keep the form names in the same shape.

Recommended Answers

All 4 Replies

Ohh..so do I, also need it. I had similar situation last week and still don't understand. I'd like to share with this problem on for further voting. I would be grateful a lots if someone will get right answer.

This is my data,

        $table_permissions = array(
            'users'=>array("create"=>1,"modify_own"=>1,"modify_any"=>1,"delete_own"=>1,"delete_any"=>1,"view"=>1),
            'users_group'=>array("create"=>1,"modify_own"=>1,"modify_any"=>1,"delete_own"=>1,"delete_any"=>1,"view"=>1),
            'news'=>array("create"=>1,"modify_own"=>1,"modify_any"=>1,"delete_own"=>1,"delete_any"=>1,"view"=>1)
        );
        $json_encode = json_encode($table_permissions);
        $json_decode = json_decode('{"users":{"create":1,"modify_own":1,"modify_any":1,"delete_own":1,"delete_any":1,"view":1},"users_group":{"create":1,"modify_own":1,"modify_any":1,"delete_own":1,"delete_any":1,"view":1},"news":{"create":1,"modify_own":1,"modify_any":1,"delete_own":1,"delete_any":1,"view":1}}', true);

how can i make of a form and save to the database

Ohh..so do I, also need it. I had similar situation last week and still don't understand. I'd like to share with this problem on for further voting. I would be grateful a lots if someone will get right answer.

you dont actually state what problem you're having be descriptive no one wants to try to guess

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.