3

I have a method in my controller as follows:

function surcharge_load_data(){ $data = $this->Surcharge_model->surcharge_load_data(); foreach($data->result_array() as $r) { echo $r->surcharge_id; } } 

The Surcharge_model method returns a result array. This is my method in the model:

function surcharge_load_data(){ $this->db->order_by('surcharge_id', 'DESC'); $query = $this->db->get('surcharges'); return $query->result_array(); } 

The data returned from the above method looks like this:

[{"surcharge_id":"37","surcharge_curr":"EUR","surcharge_name":"MNM","surcharge_unit_id":"3","surcharge_value":"43","surcharge_category_id":"3","created_timestamp":"2019-06-03 06:18:18","updated_timestamp":"2019-09-01 08:19:18"}, {"surcharge_id":"36","surcharge_curr":"EUR","surcharge_name":"TOY","surcharge_unit_id":"3","surcharge_value":"433","surcharge_category_id":"3","created_timestamp":"2019-07-09 09:21:21","updated_timestamp":"2019-09-10 09:21:21"}] 

A few questions as follows:

(1) The entire data is encapsulated in []. Does that mean it has returned one large object with array of arrays?

(2) Why do I get an error, Call to a member function result_array() on array? I thought $data is a result array. How can I access it please?

0

2 Answers 2

3

Try this, no need to use result_array() here

function surcharge_load_data(){ $data = $this->Surcharge_model->surcharge_load_data(); foreach($data as $r) {//changes echo $r['surcharge_id']; } } 

HERE, you are using result_array() so you need to use each object like $r['surcharge_id'] if you use result() then you need to use $r->surcharge_id

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Tried that previously but I get an error, Trying to get property of non-object on line, echo $r->surcharge_id;
2

From your controller you can't retrieve the result_array method because this only exists within your Model.

So, in your controller you can read the answer from your model like:

function surcharge_load_data(){ $data = $this->Surcharge_model->surcharge_load_data(); foreach($data as $r) { //just here you can retrieve your data ... } } 

Where $data is the result from the model (before returned as $query->result_array()) which your are recieving in your controller

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.