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条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?