PHP Learner
PHP Learner

Reputation: 111

undefined offset issue in if condition in php

<?php
$get_user="SELECT privillage FROM admin_user WHERE id='".$_SESSION['user_id']."'";
 // echo $get_user;
 $exe_query=mysql_query($get_user);
$row=mysql_fetch_array($exe_query);
 $privillage_data = unserialize($row['privillage']); 
                  //print_r($privillage_data);
                  //$privillage_data1=explode(",",$privillage_data);
 echo '<pre>';
print_r($privillage_data);
$count=count($privillage_data);
 //echo "Count".$count;

 if($privillage_data[0] == '1' || $privillage_data[1] == '1' || $privillage_data[2] == '1' && $count >=1)
    {
        $priv_add ="true";
        echo "add--".$priv_add."</br>";
    }
else{$priv_add ="false";}

if($count >=2 && $privillage_data[1] == '2' || $privillage_data[0] == '2' || $privillage_data[2] == '2')
    {
        $priv_edit ="true";
        echo "edit--".$priv_edit."</br>";
    }
    else{$priv_edit ="false";}

    if($count ==3 && $privillage_data[2] == '3' || $privillage_data[0] == '3' || $privillage_data[1] == '3')
     {
        $priv_delete ="true";
        echo "delete--".$priv_delete."</br>";
    }
    else{$priv_delete ="false";}

?>

i'm getting undefined offset error in if condition,when using the above code!! how to solve the offset issue!! Need in help!!kindly make me simple

Upvotes: 0

Views: 2481

Answers (2)

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

Looks like you have empty values for array index

try with isset() or empty() like

isset($privillage_data[0]) or !empty($privillage_data[0])

and so on... for all index

so your condition will be

if((!empty($privillage_data[0]) && $privillage_data[0] == '1') || (!empty($privillage_data[1]) && $privillage_data[1] == '1') || (!empty($privillage_data[2]) && $privillage_data[2] == '1') && $count >=1)

and so on for all conditions you are using

Upvotes: 3

Rachael
Rachael

Reputation: 424

Dump out the contents of privillage_data. There's an array key missing that you're expecting. Also, which line above is giving the error?

Upvotes: 1

Related Questions