Reputation: 275
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>
Upvotes: 0
Views: 2462
Reputation: 1045
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);
}
Upvotes: 2
Reputation: 57
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!
Upvotes: 0