Welcome Guest, Not a member yet? Register   Sign In
Search and results
#7

[eluser]paulc010[/eluser]
Basically to use pagination you need to do the following:

1) Get the total number of results. (usually from our model)
2) Initialize the pagination library.
3) Get the data to display. (usually from our model)
4) Display the current page of results.

The Controller:

Code:
class Search extends Controller
{
    function region_results()
    {
        $region = $this->input->post('region');
        // Get the total number of results
        $this->db->where('business_region',$region);
        $query = $this->db->get('users');
        $count = $query->num_rows();
        $query->free_result(); // We're finished with this query, so free it

        $offset = $this->uri->segment(3, 0); // get the current pagination position if it exists, else 0

        $this->load->library('pagination');

        // Create our configuration
        // The same uri segment used above to get the "offset" note: THIS ISN'T THE PAGE #
        $config['uri_segment'] = 3;
        // "Parent" url to display results
        $config['base_url'] = base_url().'search/region_results';
        // The total number of rows to be paginated, calculated above
        $config['total_rows'] = $count;
        // Choose a number of results per page
        $config['per_page'] = 10; // Number of results you want per page

        // Configure the pagination library with our config array
        $this->pagination->initialize($config);

        // Now get the actual data to display i.e. the current "page" of results
        $this->db->select('business_name');
        $this->db->where('business_region',$region);
        $this->db->limit($config['per_page'], $offset); // Only get the results for this "page"
        $query = $this->db->get('users');

        $data['title'] = 'Results for search on : ' . $region;
        $data['results'] = $query->result();
        $this->load->view('layout', $data);
    }

}

The View:

Code:
<h1>&lt;?php echo $title; ?&gt;</h1>
<ul>
&lt;?php foreach ($results as $row) { ?&gt;
    <li>&lt;?php echo $row->business_name; ?&gt;</li>
&lt;?php } ?&gt;
</ul>
&lt;?php echo $this->pagination->create_links(); ?&gt;

I typed this up off the top of my head, so apologies in advanced if I've messed it up :cheese:

Note that this isn't the greatest code in the world since we get the # of results every page and there's lots you could do to make it more efficient. You may also want the search results to be bookmarked by the user, which this method doesn't support either.

Paul


Messages In This Thread
Search and results - by El Forum - 04-25-2010, 05:00 PM
Search and results - by El Forum - 04-25-2010, 06:32 PM
Search and results - by El Forum - 04-25-2010, 06:58 PM
Search and results - by El Forum - 04-25-2010, 07:38 PM
Search and results - by El Forum - 04-26-2010, 06:22 AM
Search and results - by El Forum - 04-26-2010, 06:29 AM
Search and results - by El Forum - 04-26-2010, 07:53 AM
Search and results - by El Forum - 04-26-2010, 08:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB