douchang8219 2018-06-17 09:31
浏览 38
已采纳

如何从数组中提取数据 - PHP Codeigniter

How can I extract the field: chat_id from this array? I'm trying to put the chat_id in the id of a button in html.

Array
(
    [0] => stdClass Object
        (
            [id] => 2
            [first_name] => Armani
            [last_name] => Dee
            [contact_number] => 123456
            [email_address] => hello@gmail.com
            [home_address] => 
            [username] => armani
            [password] => 12345
            [status] => 0
            [email_confirmed] => 0
            [chat_id] => 14
            [admin_id] => 0
            [customer_id] => 2
            [partner_id] => 0
            [timestamp] => 2018-06-15 22:38:24
            [chat_exists] => 1
            [reply_id] => 208
            [message] => Hello 
            [timestamp_reply] => 2018-06-16 14:02:44
            [chat_status] => 0
        )

)

I got my output from my model:

public function chat_customer($enum) 
{
    $this->db->select('*');
    $this->db->from('customer');
    $this->db->order_by('timestamp','desc');
    $this->db->where('customer.id',$enum);
    $this->db->join('chat','chat.customer_id = customer.id','left');
    $this->db->join('chat_reply', 'chat_reply.chat_id = chat.chat_id', 'left');
    $query = $this->db->get();
    return $query->result();
}

Then I retrieved it in my controller like this:

 $customer_id = $this->input->get('message');
 $this->load->model('Crud_model');
 $data = $this->Crud_model->chat_customer($customer_id);
 echo print_r($data);

I'm trying to print the 14 on the chat_id. Thank you for your help!

  • 写回答

2条回答 默认 最新

  • dsa456369 2018-06-17 11:30
    关注

    Hope this will help you :

    Return data with $query->row(); if it always return single row

    Your model should be like this :

    public function chat_customer($enum) 
    {
        $this->db->select('*');
        $this->db->from('customer');
        $this->db->order_by('timestamp','desc');
        $this->db->where('customer.id',$enum);
        $this->db->join('chat','chat.customer_id = customer.id','left');
        $this->db->join('chat_reply', 'chat_reply.chat_id = chat.chat_id', 'left');
        $query = $this->db->get();
        return $query->row();
    }
    

    In your controller :

     $customer_id = $this->input->get('message');
     $this->load->model('Crud_model');
     $data = $this->Crud_model->chat_customer($customer_id);
     echo $data->chat_id;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题