Reputation: 71
I have a function like this:
function getOptions() {
$this->db->select('ID, Title');
$this->db->where('Parent', 0);
$options = $this->db->get('ItemCategories');
$options_arr;
$options_arr['#'] = '-- Please Select Option --';
foreach ($options as $option) {
$options_arr[$option]->ID = $option->Title;
}
return $options_arr;
}
And in foreach
line there is an error:
Message: Trying to get property of non-object
How can i fix it?
Upvotes: 0
Views: 120
Reputation: 829
you are not fetching the result. Add ->result() after get. This will solve your problem.
function getOptions() {
$this->db->select('ID, Title');
$this->db->where('Parent', 0);
$options = $this->db->get('ItemCategories')->result();
$options_arr;
$options_arr['#'] = '-- Please Select Option --';
foreach ($options as $option) {
$options_arr[$option]->ID = $option->Title;
}
return $options_arr;
}
Upvotes: 2
Reputation: 54841
I think you have a typo here:
foreach ($options as $option) {
$options_arr[$option]->ID = $option->Title;
}
should be
foreach ($options as $option) {
$options_arr[$option->ID] = $option->Title;
}
And of course consider @HashemQolami answer about $result = $this->db->result();
.
Upvotes: 1