控制器功能不会在foreach中显示返回值

时间:2018-08-07 13:18:40

标签: php codeigniter-3

我正在模型中获取查询输出并发送给控制器如果我在控制器中检查数据,例如print_r($result); 我从模型中获得输出,而我的输出是

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
)

型号代码为

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

现在我在控制器上。我正在foreach中传递输出,但是当我检查$row->phone then it's showing me

消息时:试图获取非对象的属性

`

$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);

1 个答案:

答案 0 :(得分:0)

希望这对您有帮助:

row()提取单个记录,因此您不需要像这样循环它:

$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);