0

I am using CodeIgniter. I am getting the response from model and display on the controller.

Model

//some code here
return array('secondary_data'=> $result,'primary_data' =>$result2);// sending to controller
// some code here

Controller

$result=$this->Search_model->get_search_name($cust_name);//calling model    
$data['secondary_data'] = $result['secondary_data'];
$data['primary_data'] = $result['primary_data'];

print_r($data['secondary_data']);
print_r($data['primary_data']);

output

Array
(
    [0] => stdClass Object
        (
            [member_id] => 
            [customer_id] => 
            [first_name] => qwer
            [last_name] => poiu
            [email] => 
            [member_type] => 2

        )

)
Array
(
    [0] => stdClass Object
        (
            [member_id] => 1
            [customer_id] => 0011
            [first_name] => asdasd 
            [last_name] => asdasda
            [email] => [email protected]
            [member_type] => 1

        )

)

but sometimes I am getting empty array of print_r($data['primary_data']) because of no data. Yes, it's possible.;

but in below if condition I am checking the data count count($data) > 0 and if found empty then calling the else part.

I don't want to call else part if only one array is emply. if both array is empty then it should call else part

    if (count($data) > 0)
     {
        $data['title'] = "Search";
        $data['heading'] = "Search";
        $data['content'] = $this->load->view('search',$data,true);
        $this->load->view('dashboard/dashboard',$data);
        }
       else{
       echo "NO data available";
      }

Any idea how to do this? Thanks

2
  • if(count($data) && (!empty($data['secondary_data']) || !empty($data['primary_data']))){...}else{...} Commented Nov 16, 2018 at 6:15
  • write condition for both array or Use loop and check Commented Nov 16, 2018 at 6:16

4 Answers 4

2

Check your both array separately in or condition

if (count($data['primary_data']) > 0 || count($data['secondary_data']) > 0)
{
   $data['title'] = "Search";
   $data['heading'] = "Search";
   $data['content'] = $this->load->view('search',$data,true);
   $this->load->view('dashboard/dashboard',$data);
}else{
   echo "NO data available";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this condition with OR operator

if(!empty($data['primary_data']) || !empty($data['secondary_data'])){
//One or both array have values
}else{
//Both have no values
}

2 Comments

Your code is also working for me. upvote from my side
This is short and simple solution.
0

You can use separate condition for both array, If one of them is not empty then apply If condition otherwise else.

if ( !empty($data['secondary_data']) || !empty($data['primary_data']) ) {
    $data['title'] = "Search";
    $data['heading'] = "Search";
    $data['content'] = $this->load->view('search',$data,true);
    $this->load->view('dashboard/dashboard',$data);
} else {
   echo "NO data available";
}

Comments

0

start checking from $data then sub array

if(count($data) && (isset($data['secondary_data']) && !empty($data['secondary_data'])) || (isset($data['primary_data']) && !empty($data['primary_data']))){
      ....
    }else{
      ...
    }

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.