black_belt
black_belt

Reputation: 6799

Using in_array() in PHP

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 :)

Upvotes: 0

Views: 4008

Answers (4)

Donovan
Donovan

Reputation: 926

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.

Upvotes: 0

black_belt
black_belt

Reputation: 6799

I think you could use the following method:

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

Upvotes: 0

Michael Zelensky
Michael Zelensky

Reputation: 2132

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";

Upvotes: 2

WWW
WWW

Reputation: 9870

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.

Upvotes: 1

Related Questions