Reputation: 1088
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?
Upvotes: 3
Views: 1521
Reputation: 2355
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 useresult()
then you need to use$r->surcharge_id
Upvotes: 3
Reputation: 470
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
Upvotes: 2