0

I need to modify views in an application and I have table user privileges, with a structure like this below

 id_staff |     feature        |            capabilities           
    1              reports                       view                    
    1              reports                       create   
    1              reports                       edit              
    2              reports                       view                    
    2              reports                       delete                    
    3              reports                       view       

What I need, is where user like id_staff 3 that in reports, feature only have one capabilities, like the example he just can view, can not edit, edit, or create. Then user id 3 only sees hyperlink to view the page, he cannot see hyperlink to create, edit or delete pages.

To do that, I tried to use select query, insert that to array and then compare the array value to getting the condition like above

I have tried to code like this

<?php 
  $query = $this->db->query('SELECT * FROM tblstaff_permissions WHERE staff_id='.$id.'');  
  foreach ($query->result() as $row)
  {  
    if($row['feature']=='reports' and $row['capabilities']=='view'){ 
      <a href="view.php">View</a> 
    } elseif($row['feature']=='reports' and $row['capabilities']=='delete' ){ 
      <a href="delete.php">Delete</a> 
    }
  }      
?>    

And then the page is blank

Do you know where's the error ?

Thank you

2
  • Should not it be $query->fetch_assoc() instead of $query->result()? Commented Jul 4, 2019 at 4:23
  • Hi @fifonik, it doesn't change Commented Jul 4, 2019 at 4:28

2 Answers 2

1

You could improve it by reducing the repetitive code, like the $row['feature']=='reports' code, like :

$query = $this->db->query('SELECT * FROM tblstaff_permissions WHERE staff_id='.$id.'');  
foreach ($query->result_array() as $row)
{  

    if ($row['feature']=='reports') {
        if ($row['capabilities']=='view') {
            echo '<a href="view.php">View</a>';
        } elseif ($row['capabilities']=='delete') {
            echo '<a href="delete.php">Delete</a>';
        }
    }

}

To get an array result, use the $query->result_array() instead of $query->result(), and also you misses the echo statement.

Sign up to request clarification or add additional context in comments.

Comments

1

You are using $query->result() it will give you an array of objects, not an array of array. Also, you are not echoing your links on the view. One more thing you should not use the same condition repeatedly.

In this case, your code should be -

<?php 
     $query = $this->db->query('SELECT * FROM tblstaff_permissions WHERE staff_id='.$id.'');  
     foreach ($query->result() as $row) { 
       if($row->feature =='reports'){ 
         if($row->capabilities=='view'){ 
            echo '<a href="view.php">View</a>';
         } elseif($row->capabilities == 'delete' ){ 
            echo '<a href="delete.php">Delete</a>';
         }
       }
     }      
?>   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.