PHP Code for pagination:
Controller: controller.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Significant extends CI_Controller {
function __construct() {
parent::__construct();
//load pagination library
$this->load->library('pagination');
//load post model
$this->load->model('model');
}
public function index(){
$config = array();
$config["base_url"] = base_url('significant/index/');
$config["total_rows"] = $this->model->record_count();
$config["per_page"] = 3;
$config["uri_segment"] = 3;
$config['full_tag_open'] = '';
$config['full_tag_close'] = '
';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '';
$config['first_tag_close'] = ' ';
$config['prev_link'] = '« Previous';
$config['prev_tag_open'] = '';
$config['prev_tag_close'] = ' ';
$config['next_link'] = '» Next';
$config['next_tag_open'] = '';
$config['next_tag_close'] = ' ';
$config['last_tag_open'] = '';
$config['last_tag_close'] = ' ';
$config['cur_tag_open'] = '';
$config['cur_tag_close'] = ' ';
$config['num_tag_open'] = '';
$config['num_tag_close'] = ' ';
$this->pagination->initialize($config);
if ($this->uri->segment(3) != null) {
$temp = $this->uri->segment(3);
} else {
$temp = 0;
}
//$page = ($this->uri->segment(3)) ? $this->uri->segment(4) : 0;
$page = $temp;
$data['getPost'] = $this->blog_model->loadPost($page, $config["per_page"]);
$data["pagination"] = $this->pagination->create_links();
$this->load->view('index', $data);
}
Model: (Model.php)
The Post model is used to retrieve posts data from the database. The record_count() function fetch the total number of records from posts tabl.
loadPost() function fetch records based on the limit restriction provided in the params and returns posts data as an array.
class blog_model extends CI_Model {
public function record_count() {
$query = $this->db->query("SELECT id FROM posttbl");
$total=$query->num_rows();
return $total;
}
public function loadPost($limit,$start) {
$query = $this->db->query("SELECT * FROM posttbl LIMIT $limit, $start");
return $query->result();
}
}
View: (index.php)
foreach($getPost as $post_item):
echo $post_item->title;
endforeach;
echo $pagination;