0

I have two(2) tables tblPermission and tblSubPermission which holds the permissions of the user. I wanted to create an interface like this

Example Image

the problem is I am stuck with my reference code (please see reference code below). Like in the image the title of each column should be the main permissions (tblPermission) and the check boxes are the sub permissions(tblSubPermission).

Here is my database structure:

tblPermission (
    PermissionID NVARCHAR
    PremissionDesc NVARCHAR
)

tblSubPermission (
    SubPermissionID NVARCHAR
    PermissionID NVARCHAR
    SubPremissionDesc NVARCHAR
)

Here is my reference code from my previous project that generates check boxes and arranged them by columns. See this as example

Image

The checkbox should be organized with the main permission.

$counter = 0;
$column = '';

if(!empty($righttype)){
    $sql = $conn->prepare('SELECT RightID, RightDescription FROM tblRight WHERE RightType = :righttype ORDER BY LENGTH(RightID), RightID');
    $sql->bindValue(':righttype', $righttype);
}
else{
    $sql = $conn->prepare('SELECT RightID, RightDescription FROM tblRight ORDER BY LENGTH(RightID), RightID');
}

if($sql->execute()){
    $count = $sql->rowCount();
    $fetchall = $sql->fetchAll();
    $range = range(1, $count);
    
    if(count($range) > 0){
        $divideCount = ceil(count($range)/2);
        $results = array_chunk($fetchall ,$divideCount);

        foreach($results as $result){
            $column .= '<div class="col-lg-6">';

            foreach($result as $fetchall){
                $counter++;
                
                $rightid = $fetchall['RightID'];
                $rightdesc = $fetchall['RightDescription'];
                $class = "right";
                    
                $column .= '<div class="col-lg-auto">
                                <div class="custom-control custom-checkbox mb-3">
                                    <input type="checkbox" class="custom-control-input right-checkbox '. $class .'" id="'. $rightid .'" value="'. $rightid .'">
                                    <label class="custom-control-label" for="'. $rightid .'">'. $rightdesc .'</label>
                                </div>
                            </div>';
            }
                         
            $column .= '</div>';
        }

        return $column;
    }
}
else{
    return $sql->errorInfo();
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Join the two tables and order the results by `PermissionDesc, SubPermissionDesc`. Whenever `PermissionDesc` changes, write a new heading. Then write `SubPermissionDesc` after the checkbox. – Barmar Nov 03 '20 at 01:20
  • @Barmar the problem is there will be multiple permissiondesc if you join the two table or am I interpreting this wrong? can you show me your sample query? –  Nov 03 '20 at 01:24
  • That's why you only show the header when PermissionDesc changes, not every time through the loop. – Barmar Nov 03 '20 at 01:26
  • `SELECT PermissionDesc, SubPermissionDesc from tblPermission p JOIN tblSubPermission sp ON p.PermissionID = sp.PermissionID ORDER BY PermissionDesc, SubPermissionDesc`. – Barmar Nov 03 '20 at 01:27
  • You save PermissionDesc in a variable. Then when processing the next row you check if `$row['PermissionDesc'] != $last_permissionDesc` – Barmar Nov 03 '20 at 01:28
  • @Barmar Can you show me the code? I do not know how can I store the PermissionDesc to $last_permissionDesc –  Nov 03 '20 at 01:47
  • Did you even think about it? `$last_permissionDesc = $row['PermissionDesc'];` – Barmar Nov 03 '20 at 01:48
  • @Barmar yes but is it the same with `$row['PermissionDesc'] != $row['PermissionDesc'];`? –  Nov 03 '20 at 01:52
  • You assign it AFTER you do the check, of course. – Barmar Nov 03 '20 at 01:53
  • See the code in the linked question. – Barmar Nov 03 '20 at 01:54

0 Answers0