how to add pagination to this

2019-08-04 05:11发布

问题:

I'm trying to add pagination to my search results I get on my view page. I'm pretty much confused with getting url like..

localhost:81/profile/index.php/main/search?query=vi

What i'm trying to get is:

localhost:81/profile/index.php/main/search?query=vi&off=5

with the offset value, when clicked on 1,2,3 pagination links.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller{

function index()
{     


    $this->load->view('main_view');

}

function search()
{

    $off = $_GET['off'];

    $query = $_GET['query'];

    $min_length = 1;



    if(strlen($query) >= $min_length)
    {
        $query_str = "SELECT * FROM customers WHERE customerName LIKE '%$query%' LIMIT 10 OFFSET '$off'";
        echo $query_str;
        $result = $this->db->query("SELECT * FROM customers WHERE customerName LIKE '%$query%' LIMIT 10 OFFSET $off")->result_array();

        echo "<table border='1' cellpadding='10'>";
        echo "<tr><th>Number</th><th>Name</th><th>address</th><th>phone</th></tr>";

        foreach($result as $data)
            {
                echo "<tr></tr>";       
                echo "<td>".$data['customerNumber']."</td>"."<td>".$data['customerName']."</td>"."<td>".$data['addressLine1']."</td>".
                "<td>".$data["phone"]."</td>";

            }


    }
    else
    { 
        echo "Minimum length is ".$min_length;
    }

    $this->load->view('main_view');
}}
?>

回答1:

If only CodeIgniter had a great user guide, and a pagination library.

Take a look at the above page. You then want to filter your records when selecting them based on the parameters in the URL.



回答2:

Check this link. Where i answered a how to do pagination after u get result set from your database in a brief way with code.

Show limited no. of page links in codeigniter pagination