Question

Today, I've been working on a page with a permission system. Everything is echoed out, and the checkboxes look a lot like this:

<table>
    <tr>
        <td><input type="checkbox" name="permissions[]" value="main" /></td>
        <td>Access Admin Panel</td>
    </tr>
    <tr>
        <td><input type="checkbox" name="permissions[]" value="config.admin-accounts" /></td>
        <td>Configure Admin Accounts</td>
    </tr>
    <tr>
        <td><input type="checkbox" name="permissions[]" value="config.admin-roles" /></td>
         <td>Configure Admin Roles</td>
    </tr>
    <tr>
        <td><input type="checkbox" name="permissions[]" value="world1.ban.permanent" /></td>
        <td>[World 1]: Ban players permanently</td>
    </tr>
    <tr>
          <td><input type="checkbox" name="permissions[]" value="world1.ban.normal" /></td>
          <td>[World 1]: Ban players</td>
    </tr>
    <tr>
          <td><input type="checkbox" name="permissions[]" value="world2.ban.permanent" /></td>
          <td>[World 2]: Ban players permanently</td>
    </tr>
    <tr>
          <td><input type="checkbox" name="permissions[]" value="world2.ban.normal" /></td>
          <td>[World 2]: Ban players</td>
    </tr>
</table>

On my website, there are multiple worlds, servers, or whatever you would like to call them. Also, I would like to be able to actually have categories within the rights. Which basically means that I want a multi-dimensional array with as many sub-categories as I want without changing the script. What I've done so far:

<?php
    $permissions = $_POST['permissions'];
    $resArray = array(); //array to store everything in
    foreach($permissions as $val){
        $keys = explode(".", $val);
        $last_key = $keys[count($keys)-1];

        foreach($keys as $key => $value) {
            if($last_key != $value){
                //code here
            }else{
                //code here
            }
        }
    }
?>

What I want the result to be:

Array
(
    [main] => true
    [config] => Array
        (
            [admin-accounts] => true
            [admin-roles] => true
        )
    [world1] => Array
        (
            [ban] => Array
                (
                    [permanent] => true
                    [normal] => true
                )
        )

    [world2] => Array
        (
            [ban] => Array
                (
                    [permanent] => true
                    [normal] => true
                )
        )
)

Basically, I want the array value to be true if the box is checked, and false if it's not. It's not important for me to store permissions that aren't checked, because it will return false nonetheless in my php checks. I'm storing the permissions using serialize() and unserialize() in a row in the database.

Était-ce utile?

La solution

to have multidimensional array for PHP checkbox you can simply post like

<input type="checkbox" name="permissions[0][]" value="main" />
<input type="checkbox" name="permissions[1][]" value="main" />
<input type="checkbox" name="permissions[2][]" value="main" />

and when you receive values you can define permissions[0] to be main permissions[1] to be config etc with if condition

Hope that answers your question

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top