Codeigniter – undefined variable-Collection of common programming errors

I am getting this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: results

Filename: views/body.php

Line Number: 75

My Model

function get_results($search_term='default')
{
    $this->db->select('*');
    $this->db->from('books');
    $this->db->like('title', $search_term);

    $query = $this->db->get();

    return $query->result_array();
}

My Controller

function execute_search(){


    $this->load->model('books_model');
    $search_term = $this->input->post('search');

    $data['results'] = $this->books_model->get_results($search_term);

   $this->load->view('body', $data);


}

My View

Search Results




    




No books found

 



I do not understand because I have called the variable 'results' in my controller yet it is not working in the view. Thanks in advance for any help.

  1. Try not returning the query result as an array until the for loop.

    Your Model:

    function get_results($search_term='default')
    {
        $this->db->select('*');
        $this->db->from('books');
        $this->db->like('title', $search_term);
    
        $query = $this->db->get();
    
        return $query;
    }
    

    No changes in your Controller

    Your View

    Search Results

    
    
    
        
    
    
    
    
    

    No books found

     
    
    
    
    

Originally posted 2013-11-09 22:46:05.