Welcome Guest, Not a member yet? Register   Sign In
Pagination class and Search Function help!
#1

[eluser]Devon Lambert[/eluser]
I've been at this one for a few hours now and thought it couldn't hurt to hit up the CI community for some much needed Ninja Style assistance.

Here's the deal:

I've created a quick and dirty CI search function and have made use of it in my controller and view. I've whipped up a table with a little bit of css styling that is calling in my search results. I do not want to break this setup and I do not want to shift any view code into my controller or model, besides, that wouldn't be the right MVC thing to do would it?

The main issue is that when I click on my 2nd paginated link, I receive the following error:

Code:
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: search.php

Line Number: 14

Here is the code I have so far:

CONTROLLER:

Code:
<?php

class search extends Controller {

    function search()
    {
            parent::Controller();
        $this->load->model('mdl_search');
    }


    function index()
    {
        $data['title'] = "Better Search";
        $keyword = $this->input->xss_clean($this->input->post('keyword'));
            $data['count_results'] = $this->mdl_search->countResults($keyword);
        $this->load->library('pagination');

        $config['base_url'] = base_url().'search/index';

        $config['total_rows'] = $data['count_results'];

        $config['uri_segment'] = '2';

        $config['per_page'] = '10';

        $this->pagination->initialize($config);

             $offset = $this->uri->segment(3);

        $data['search_results'] = $this->mdl_search->getSearchResults($keyword, $config['per_page'], $offset);                

        $this->load->view('search', $data);
    }
}
?>

MODEL:

Code:
<?php

class mdl_search extends Model {



    function mdl_search()

    {

        parent::Model();

    }




    function getSearchResults ($keyword, $limit, $offset)

    {

        $keyword = trim($keyword);



        if($keyword != ''){



            $this->db->like('name', $keyword);

            $this->db->or_like('desc', $keyword);

            $this->db->or_like('cat', $keyword);

            $this->db->or_like('instructions', $keyword);

            $this->db->orderby('name', 'ASC');

            $result = $this->db->get('games',  $limit, $offset);

            $output = $result->result_array();

            return $output;

        }

        

        else {return 0; }

    }

    

    function countResults ($keyword){

        

        $keyword = trim($keyword);



        $this->db->like('name', $keyword);

        $this->db->or_like('desc', $keyword);

        $this->db->or_like('cat', $keyword);

        $this->db->or_like('instructions', $keyword);

        $this->db->orderby('name', 'ASC');

        $this->db->from('games');

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

        

        return $query;

    }

}

?>

VIEW:

Code:
<div class="search-container">

        <h2>&lt;?= $title;?&gt;</h2>

        <div class="pagination">&lt;?php echo $this->pagination->create_links(); ?&gt;</div>

        
<table class="search-results">

                <tr>

                    <th><a href="#"><span>Game Title</span></a></th>

                    <th><a href="#"><span>Players</span></a></th>

                    <th><a href="#"><span>Rating</span></a></th>

                    <th><a href="#"><span>Reviews</span></a></th>

                    <th><a href="#"><span>Play Type</span></a></th>

                </tr>

                

                    &lt;? foreach ($search_results as $search_result): ?&gt;

                        <tr>

                            <td><a href="#">&lt;?php echo $search_result['name'];?&gt;</a><br />&lt;?php echo $search_result['desc'];?&gt;</td>

                            <td>1,262</td>

                            <td><b>5.00</b><br />out of 5.0</td>

                            <td><span class="up">72%</span><br />491 reviews</td>

                            <td></td>

                        </tr>

                    &lt;? endforeach; ?&gt;

        </table>

    </div>

So far, I can get the search function to work just fine, but when I combine this with the pagination and go to any other page besides the first search page, it's as if the post data I went searching for gets thrown out.

Please, any help is definitely appreciated in getting this working. :-)
#2

[eluser]Michael Wales[/eluser]
Quote:it’s as if the post data I went searching for gets thrown out.
It does. Smile

When a user searches, they are sending a post request to the controller. Now, when they click the pagination link, a whole new request is being sent to that controller's method. There's no $_POST array and nothing there to tell your search function what to search for.

A few options:
1) My favorite: include the search term as a segment within the URI.
2) Use a Javascript library to unobtrusively stop the default action on the pagination links and rather, submit a hidden form with the original search term and the offset segment from the pagination link).
3) Probably the easiest: Set a session variable to hold the search term and use it if there is no term within the $_POST array.
#3

[eluser]Devon Lambert[/eluser]
Michael Wales you are my hero!

:-)
#4

[eluser]Cambo[/eluser]
[quote author="Michael Wales" date="1215423721"]
Quote:it’s as if the post data I went searching for gets thrown out.
It does. Smile

When a user searches, they are sending a post request to the controller. Now, when they click the pagination link, a whole new request is being sent to that controller's method. There's no $_POST array and nothing there to tell your search function what to search for.

A few options:
1) My favorite: include the search term as a segment within the URI.
2) Use a Javascript library to unobtrusively stop the default action on the pagination links and rather, submit a hidden form with the original search term and the offset segment from the pagination link).
3) Probably the easiest: Set a session variable to hold the search term and use it if there is no term within the $_POST array.[/quote]

Hi Michael
I am trying pagination by passing uri segments to the controller, rather than post data.
I am having the same issue so obviously I have to recreate the uri but I am not sure where.
Can you give an example for your No 1 option.
Thanks
Cambo
#5

[eluser]Cambo[/eluser]
Hi Michael
Relax, I solved it. :coolsmile:
(after a couple hours of r&dWink
Here's what I ended up with.
Hope it helps some-one out.
Regards
Cambo

Code:
$total_rows = count($this->objCars->searchCars($marque,$model,$state,$retail,$body,$sortby));
        
        // set up pagination
        $this->load->library('pagination');
        $uri_string = $this->uri->slash_segment(1);
        for ($n = 2; $n < 9; $n++)
        {
        $uri_string .= $this->uri->slash_segment($n);
        }
        $config['base_url'] = base_url().$uri_string;
        $config['total_rows'] = $total_rows;
        $config['per_page'] = '10';
        $config['uri_segment'] = '9';
        $this->pagination->initialize($config);
        
        $limit = $config['per_page'];
        $offset = $this->uri->segment(9, 0);
        
        $data['results'] = $this->objCars->searchCars($marque,$model,$state,$retail,$body,$sortby,$limit,$offset);

        $data['pageLinks'] = $this->pagination->create_links();
        //




Theme © iAndrew 2016 - Forum software by © MyBB