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 数据库原理及应用上机练习题
  • ¥30 征集Python提取PDF文字属性的代码
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 (求远程解决)深信服vpn-2050这台设备如何配置才能成功联网?