简体   繁体   中英

How to Create pagination in codeigniter

Hi I have implemented Pagination in PHP Code But it is not Working while clicking on the pagination links.It is displaying the same data for all the pages.Here is the code.

Controller:

class Testimonial extends CI_Controller {
function __construct() { 
        parent::__construct();
        //here we will autoload the pagination library
        $this->load->library('pagination');
    }

public function index()
{
    $this->load->model('testimonial_model');
    $config = array();
    $config["base_url"] = base_url('testimonial/index');
    $config['total_rows'] =   $this->db->count_all("testimonials");//here we will count all the data from the table
    $config['per_page'] = 6;//number of data to be shown on single page
    $config["uri_segment"] = 2;
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
    $data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();//create the link for pagination
    $data['mainpage'] = "testimonial";
    $this->load->view('templates/template',$data);
}

Model:

class Testimonial_model extends CI_Model
{    
function get_all_testimonials($limit, $start)
{
    $this->db->limit($limit, $start);
    $this->db->select('T.*');
    $this->db->from('testimonials AS T');
    $this->db->where(array('T.status'=>1));
    $q = $this->db->get();
    if($q->num_rows()>0)
    {
        return $q->result();
    }
    else 
    {
        return false;
    }
}   
}

View:

<div class="pagination"><?php echo $links; ?></div>     

Try following will may help you,

public function index()
{
   $this->load->model('testimonial_model');
   $config = array();
   $config["base_url"] = base_url('testimonial/index');
   $config['total_rows'] =   $this->db->count_all("testimonials");//here we will count all the data from the table
   $config['per_page'] = 6;//number of data to be shown on single page
   $config["uri_segment"] = 2;
   $this->pagination->initialize($config);
   $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
   $data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], (($page-1)*$config["per_page"]));
   $data["links"] = $this->pagination->create_links();//create the link for pagination
   $data['mainpage'] = "testimonial";
   $this->load->view('templates/template',$data);
}

I cant comment so I just make this an answer,

Here http://bootsnipp.com/snippets/featured/rounded-pagination

This is what I use in making my pagination! and there is alot more of it! I also use CI as my framework!

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