2

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] => [email protected]
            [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!

11
  • 1
    $var[0]->chat_id Commented Jun 17, 2018 at 9:33
  • @Metalik Hi thank you for answering my question but I tried that already and I got an error that states undefined offset 0. Commented Jun 17, 2018 at 9:35
  • @pradeep Hi! I tried these but still no luck: echo $data['0']['chat_id']; echo $data['chat_id']; echo $data->chat_id; Commented Jun 17, 2018 at 9:38
  • try with this echo $data[0]->chat_id; Commented Jun 17, 2018 at 9:40
  • @pradeep Thank you very much but still I got an error of I got an error that states undefined offset 0. Commented Jun 17, 2018 at 9:44

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest to use a foreach loop if you are going to get multiple rows in your query.

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

This will make sure you iterate over all the rows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.