简体   繁体   中英

Codeigniter Pagination

There is a bit of problem in my code which i am not able to solve. I m using CI 1.7.2. I have implemented the CI Pagination into the system correctly. The results are displayed fine but the links in the pagination are not rendering correctly.

For eg. If i click on the page 2 then the results are displayed as per the 2nd Page but the current link at pagination numbers remains 1 which should change to 2.

Here is the code that has been implemented

$total = $this->bmodel->countResultsBanner();

    $data['total'] = $total;

    $uri_segment = $this->uri->segment(4);

    if($uri_segment == 0 || empty($uri_segment)){
    $uri_segment = 0;
    }

    $perPage = 5;



    $config['base_url'] = base_url()."index.php/modules/banner/index";

    $config['total_rows'] = $total;

    $config['per_page'] = $perPage;

    $config['num_links'] = 4;

    //$config['cur_tag_open'] = '<b><span class="current_page">';

    //$config['cur_tag_close'] = '</span></b>'; 

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

    $result = $this->bmodel->getAllBanners($perPage,$uri_segment);


    $data['result'] = $result;

thanks in advance.

J

Heyy,

I also faced the same problem. In the end, solution turned out to be very simple. :)

by default CI assumes that uri segment used for pagination is (3). Which in your case, for you (i am assuming shamelessly) is incorrect.

$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;

$config['uri_segment'] = 3; /* segment of your uri which contains the page number */

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

Hope this solves your problem

ok... try this...

$total = $this->bmodel->countResultsBanner();

    $data['total'] = $total;

/* Comment out this part
        $uri_segment = $this->uri->segment(4);

        if($uri_segment == 0 || empty($uri_segment)){
        $uri_segment = 0;
        }
    */
    $perPage = 5;



    $config['base_url'] = base_url()."index.php/modules/banner/index";

    $config['total_rows'] = $total;

    $config['per_page'] = $perPage;

    $config['num_links'] = 4;

    //$config['cur_tag_open'] = '<b><span class="current_page">';

    //$config['cur_tag_close'] = '</span></b>'; 

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

/*Change the following line*/

 $result = $this->bmodel->getAllBanners($perPage,$this->uri->segment(5));


    $data['result'] = $result;
$this->load->library('pagination');
$config['base_url']="http://localhost/CodeIgniter/pagination";

$config['per_page']=2;

$config['total_rows']=  $this->db->get('record')->num_rows();
$this->pagination->initialize($config);
$data['query']=  $this->db->get('record',$config['per_page'],  $this->uri->segment(3));
$this->load->view('pagination',$data);
$config['uri_segment'] = num; /* where num is the uri segment where you have page number */

Try this , it might help.

class Admin_model extends CI_Model {

public function __construct() {
    parent::__construct();
}
public function get_product($search, $page, $perpage) {
    $page = $page - 1;
    $page < 0 ? $page = 0 : $page = $page;
    $from = $page * $perpage;
    $query = $this->db
            ->select('*')
            ->from('tbl_product')
            ->limit($perpage, $from)
            ->get();
    return $query->result();
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM