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

时间:2018-06-17 09:31:52

标签: php codeigniter codeigniter-3

如何从此数组中提取字段:chat_id?我正在尝试将chat_id放在html中按钮的id中。

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
        )

)

我的模型输出了我的输出:

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();
}

然后我在我的控制器中检索它:

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

我正在尝试在chat_id上打印14。  谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

希望这会对您有所帮助:

如果数据始终返回单行

,则返回$query->row();的数据

你的模型应该是这样的:

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();
}

在您的控制器中:

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

答案 1 :(得分:0)

如果您要在查询中获取多行,我建议使用foreach循环。

foreach($data as $d){
  echo $d['column_name'];
}

这将确保您遍历所有行。