begum
begum

Reputation: 71

CodeIgniter- Trying to get property of non-object

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

Answers (2)

SMNTB
SMNTB

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

u_mulder
u_mulder

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

Related Questions