0

I am getting my query output in the model and sending to controller If I check the data in the controller like print_r($result); I am getting the output from my Model and My output is

stdClass Object
(
    [membership_id] => 11
    [member_id] => 10
    [customer_id] =>21
    [membership_approve] => 1
    [membership_status] => 1
    [profile_pic] => 
    [first_name] => Juvin
    [middle_name] => kumar
    [last_name] => choudhary
    [email] => juvin@gmail.com
    [dob] => 2018-07-01
    [phone] => 01236541254
    [is_Approved] => 1
)

Model code is

if ($result) {
  foreach($result as $result_set ){
     if($result_set->membership_status == 1)
      {
      $result2=$this->db->select('*')
            ->from('membership_details')
            ->join('members','members.member_id = membership_details.member_id')
            ->group_start()
                ->where('members.is_Approved',1)
                ->where('membership_details.membership_status', 1)
                ->where('membership_details.member_id',$result_set->member_id)
              ->group_end()

             ->get()
            ->row();
          return $result2;
      }
      else 
      {
        return 0;
      }
    }

  }
  else{echo "Not working";}

Now I am on the controller. I am passing the output in the foreach but when I check $row->phone then it's showing me

Message: Trying to get property of non-object

`
$result=$this->Search_model->check_membership($customer_name,$customer_mobile); 
    $arr_result2 = array();
    if (count($result) > 0)
         {
            print_r($result);// It's displaying the same output which I aaded in the question.
            foreach($result as $row){
                print_r($row->customer_id);// not getting customer id
                $arr_result2[] = array(
                    "profile_pic" => $row->profile_pic,
                    "name" => $row->first_name.' ' .$row->last_name,
                    "phone" => $row->phone
                );
            }
            }
         else{echo "No data availble";}
         //print_r($arr_result2);
        echo json_encode($arr_result2);
user9437856
  • 2,360
  • 2
  • 33
  • 92
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – AymDev Aug 07 '18 at 13:20
  • 1
    `row()` fetches single record so u don't need to loop, instead use `result()` for multiple records and then use foreach loop – Pradeep Aug 07 '18 at 13:25
  • @pradeep, Yes, I got it. I used row() in the model. I have to use result(). – user9437856 Aug 07 '18 at 13:26
  • To expand on @pradeep's comment, since it's not an array, but an object, it's keys/values would be the ones being iterated. – FirstOne Aug 07 '18 at 13:28
  • @pradeep, So I have to remove the foreach from my contoller and and use "profile_pic" => $result->profile_pic. Is it right? – user9437856 Aug 07 '18 at 13:29
  • yes it is right see my answer for some help – Pradeep Aug 07 '18 at 13:33

1 Answers1

0

Hope this will help you :

row() fetches single record so u don't need to loop it do like this :

$result = $this->Search_model->check_membership($customer_name,$customer_mobile); 
$arr_result2 = array();
if (count($result) > 0)
{
    print_r($result);// It's displaying the same output which I aaded in the question.
    $arr_result2[] = array(
        "profile_pic" => $result->profile_pic,
        "name" => $result->first_name.' ' .$result->last_name,
        "phone" => $result->phone
    );
}
else
{
    echo "No data availble";
}
 //print_r($arr_result2);
echo json_encode($arr_result2);
Pradeep
  • 9,667
  • 13
  • 27
  • 34