0

I am trying to display menus and give user rights to my users based on database value using Codeigniter. But its not working, may be the problem is in the view file. I have the following table.

Table Name: module_permission

   id   user_id   delete     add    edit
   1       1        0         1      1

My Controller:

$this->load->model('mod_module_permission');
$data['permit']= $this->mod_module_permission->permission($user_id);

My Model:

  $this->db->select('*');
    $this->db->from('module_permission');
    $this->db->where('user_id', $user_id);                                          
    $getData = $this->db->get('');
    if($getData->num_rows() > 0)
    return $getData->result_array();
    else
return null; 

My View :

    if ( in_array(add=>'1',$permit)) {echo "YES";} else {echo "NO";}

Could you please tell me what change in the view file could make the problem solved?

Thanks :)

2
  • If add=>'1' is supposed to be an array, use array(add=>'1') Commented Jun 1, 2012 at 15:59
  • 1. Read the docs on in_array(). 2. I don't think you want in_array(), I think you want to say if (isset($permit['add']) && $permit['add'] == 1) { ... } Commented Jun 1, 2012 at 16:01

4 Answers 4

2

obviously, the problem is with in_array() function usage

check the in_array() manual:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

The first parameter should be string or whatever, and the second parameter is an array being searched through.

Besides, in the first parameter you are wrongly trying to define an array, the array structure block should be similar to that:

array('key'=> 'value')

(strings are in colons)

Try this:

echo $tmp = ($permit == '1') ?  "YES" : "NO";

or

$arr = array('key'=>'1');
echo $tmp = (in_array($permit, $arr)) ?  "YES" : "NO";
Sign up to request clarification or add additional context in comments.

Comments

1

You are using in_array() incorrectly. It's in_array($needle, $haystack) where $needle is a single variable of some type. You should try in_array('1', $permit) (if $permit is actually an array).

The above answer only applies if in_array() is the appropriate solution to your problem, of course.

Comments

0

I think you could use the following method:

 <?php foreach ($permit as $rows){ 
    if ($rows['add']==1) {echo "YES";} else {echo "NO";}
} ?>                

Comments

0

Looking at you're question. It seems that you're not sure what result_array() returns. In your case, it will return the following:

array (
    array (
        "id" => 1,
        "user_id" => 1,
        "delete" => 1,
        "add" => 1,
        "edit" => 1
    )
)

So when you try to do an in_array() you are looking at the outer most array and you're if statement will never pass. However, the simple fix for your problem would be to change you're Model and update this line from:

return $getData->result_array();

to

return $getData->result_array()->row();

Then in you're view change this line:

if ( in_array(add=>'1',$permit)) {echo "YES";} else {echo "NO";}

to

if ($permit->add == 1) {echo "YES";} else {echo "NO";}

That is just one way to work with you're code to get the results you want.

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.