【问题标题】:How to Extract Data from Array - PHP Codeigniter如何从数组中提取数据 - PHP Codeigniter
【发布时间】:2018-06-17 09:31:52
【问题描述】:

如何从该数组中提取字段: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。 感谢您的帮助!

【问题讨论】:

  • $var[0]->chat_id
  • @Metalik 您好,感谢您回答我的问题,但我已经尝试过了,但出现了一个错误,指出未定义的偏移量 0。
  • @pradeep 你好!我尝试了这些但仍然没有运气: echo $data['0']['chat_id'];回声 $data['chat_id']; echo $data->chat_id;
  • 试试这个echo $data[0]->chat_id;
  • @pradeep 非常感谢,但我仍然收到一个错误,指出未定义的偏移量 0。

标签: php codeigniter codeigniter-3


【解决方案1】:

希望对您有所帮助:

如果总是返回单行,则返回 $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;

【讨论】:

    【解决方案2】:

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

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

    这将确保您遍历所有行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多