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 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU