Welcome Guest, Not a member yet? Register   Sign In
Search Error keeps appearing
#1

[eluser]clintonbeattie[/eluser]
Hi,

I am working through the Wrox Codeigniter book and keep getting this error when searching(I'm on page 126)...

A PHP Error was encountered

Severity: Notice
Message: Undefined variable: results
Filename: views/search.php
Line Number: 5

This is my search.php file contents...

Code:
<div id='pleft'>
<h2>Search Results</h2>
      
&lt;?php
if (count($results)) {
    foreach ($results as $key => $list){
        echo "<img src='".$list[' border='0' align='left' />\n";
        echo "<h4>";
        echo anchor('welcome/product/'.$list['id'],$list['name']);
        echo "</h4>\n";
        echo"<p>".$list['shortdesc']."</p><br style='clear:both'/>";    
    }
}else{
    echo "<p>Sorry, no records were found to match your search term.</p>";
}
?&gt;
</div>

All others files are as written in the book and have been double checked with the downloaded files.

Can anyone shed any light on this?

Thanks,
C

PS Here are my model view code and controller code...

Code:
function search($term){
        $data = array();
        $this->db->select('id,name,shortdesc,thumbnail');
        $this->db->like('name',$term);
        $this->db->orlike('shortdesc',$term);
        $this->db->orlike('longdesc',$term);
        $this->db->orderby('name','asc');
        $this->db->where('status','active');
        $this->db->limit(50);
        $Q = $this->db->get('products');
        if ($Q->num_rows() > 0){
           foreach ($Q->result_array() as $row){
             $data[] = $row;
           }
        }
        $Q->free_result();    
        return $data;
    }



Code:
function search() {
        if ($this->input->post('term')) {
            $search['results'] = $this->MProducts->search($this->input->post('term'));
        } else {
            redirect('welcome/index', refresh);
        }
        $data['main'] = 'search';
        $data['title'] = "Claudia's Kids | Search Results";
        $data['navlist'] = $this->MCats->getCategoriesNav();
        $this->load->vars($data);
        $this->load->view('template', $data);
    }
#2

[eluser]Armchair Samurai[/eluser]
As per the error message, you're not passing any variable called $results to the view.

Try changing

Code:
$search['results'] = $this->MProducts->search($this->input->post('term'));

to

Code:
$data['results'] = $this->MProducts->search($this->input->post('term'));

in your controller. If you're copying from a book, looks like an error on the author's part.
#3

[eluser]clintonbeattie[/eluser]
Thanks for that! Fixed the problem. Will know for future.




Theme © iAndrew 2016 - Forum software by © MyBB