0

Seems like a common occurance this error on here, Looking at the answers I still cant quite work out why I'm getting an error.

I am getting the error

Fatal error: Call to a member function result() on a non-object on line 83

The line in question relates to this function in the Controller - This is what's creating the error.

$this->view_data['categories'] = $my_categories->result();

The function is below..

function _load_search_options()
{
    // Get all the categories for the advanced search page
    $my_categories = $this->Categories_model->get_all();
    $this->view_data['categories'] = $my_categories->result();

    // Get all the PRIMARY colours from teh tbl_colour_options
    $my_colour_options = $this->Colours_model->get_all_primary();
    $this->view_data['colour_options'] = $my_colour_options->result();


    // Get all the colours from teh tbl_colour_options
    $my_colour_options_all = $this->Colours_model->get_all();
    $this->view_data['colour_options_all'] = $my_colour_options_all->result();
}

My Model is as follows...

function get_all()
{
    $query_str = "
        SELECT *
        FROM categories
        WHERE CATEGORIES_parent_id = 0
    ";

    $results = $this->db->query($query_str);
    $parents = $results->result();

    foreach ($parents as $parent)
    {
        $children = array();

        $query_str = "
            SELECT *
            FROM categories
            WHERE CATEGORIES_parent_id = '$parent->CATEGORIES_id'
        ";

        $children_results = $this->db->query($query_str);
        $children_results = $children_results->result();

        foreach($children_results as $children_result)
        {
            $children[$children_result->CATEGORIES_id] = $children_result->CATEGORIES_title;
        }

        $categories[$parent->CATEGORIES_title] = $children;




    }

    return $categories;
}

It's worth noting that running the SELECT query in MySQL on its own brings through some results.

tereško
  • 58,060
  • 25
  • 98
  • 150
StuBlackett
  • 3,789
  • 15
  • 68
  • 113

1 Answers1

1

The function get_all doesn't return a database object (resource).

The $categories variable in the get_all function is defined as an array.

You cannot access it as an object or call the results function.

Yan Berk
  • 14,328
  • 9
  • 55
  • 52