Pagination In CodeIgniter With Example

Pagination In CodeIgniter With Example

Today i am showing how can we create pagination in codeigniter for website home page, In this tutorial we used CodeIgniter pagination class by initializing pagination library. if you developing any blog or listing website then i am sure you have to create pagination for website home page also.

Pagination is one concept used for web applications. Wherever we have a hug of data and need to show them, then pagination allows you to navigate from one page to another page, at that time we require pagination.

In the same way CodeIgniter provides very efficient way to handle it with its integrated support. In this tutorial, we will see through an example, how we can implement CodeIgniter pagination and enhance it further as per our need.

Create Controller name of “Welcome.php” inside (application/controllers) folder

“Welcome.php” controller is already exist inside controller folder so you don’t have need to create it.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
* Author: Mukesh Jakhar
* Description: Pagination Controller class with name of "Welcome.php"
*/
class Welcome extends CI_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model('Home_model');
		$this->load->library('pagination');
	}
	public function index()
	{ 
		$total_rows = $this->Home_model->total_rows();
		$config = array();
		$config["base_url"] = base_url(); 
		$config["total_rows"] = $total_rows;
		$config["per_page"] = 1;
		$config['use_page_numbers'] = TRUE;
		$config['num_links'] = $total_rows;
		$this->pagination->initialize($config);
		$data['pagi_links'] = $this->pagination->create_links();
		if($this->uri->segment(1)){
			$page = ($this->uri->segment(1)) ;
		}
		else{
			$page = 1;
		}
	
		$data['res'] = $this->Home_model->get_posts($config["per_page"], '1');
		$this->load->view('welcome_message', $data);
	}
	public function pagination($pagenum='')
	{
		$total_rows = $this->Home_model->total_rows();
		$config = array();
		$config["base_url"] = base_url(); 
		$config["total_rows"] = $total_rows;
		$config["per_page"] = 1;
		$config['use_page_numbers'] = TRUE;
		$config['num_links'] = $total_rows;
		$this->pagination->initialize($config);
		$data['pagi_links'] = $this->pagination->create_links();

		if($this->uri->segment(1)){
			$page = ($this->uri->segment(1)) ;
		}
		else{
			$page = 1;
		}

		$data['res'] = $this->Home_model->get_posts($config["per_page"], $page);
		$this->load->view('welcome_message', $data);
	}
}
?>

Create model name of “Home_model.php” inside (application/models) folder

Create a model called Home_model.php. in it copy and past below code and save it to your application/models/ directory:
Inside this model we are performing database opration such a get the total number of posts using “total_rows()” function and fetch posts using “get_posts” funtion.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* Author: Mukesh Jakhar
* Description: Pagination model class
*/
class Home_model extends CI_Model{
	function __construct() {
		$this->load->database();
		parent::__construct();
	}

	public function total_rows()
	{
		$query = $this->db->query("SELECT * FROM `posts`");
		return $query->num_rows();
	}
	public function get_posts($perpage, $pagenum)
	{
		if($pagenum>1)
		{
			$limit = $perpage;
			$start = $perpage * ($pagenum-1);
		}
		else
		{
			$limit = $perpage;
			$start = 0;
		}

		$query = $this->db->query("SELECT * FROM `posts` LIMIT $start, $limit");
		return $query;
	}
}

Create view name of “welcome_message.php” inside (application/views) folder

“welcome_message.php” already exist inside views folder so you don’t have to create. in “welcome_message.php” copy and past below code and save it to your application/views/ directory:

<?php
/*
Author: Mukesh Jakhar
Description: Home page pagination view
*/
defined('BASEPATH') OR exit('No direct script access allowed');
$this->load->helper('url');
?>
<!DOCTYPE HTML>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>User registration form</title>
	<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<?php
foreach($res->result() as $tmp)
{
	echo $tmp->name."<br>";
}
echo $pagi_links;
?>
</body>
</html>

Configure routing file

You can find routes.php file inside “application/config/” folder, open routes.php file, copy and past below code and save it.

$route['default_controller'] = 'welcome';
$route['(:any)'] = 'welcome/pagination/$1'; 

Tags : How to do pagination using codeigniter, Pagination In CodeIgniter With Example, Pagination with CodeIgniter, How to use Codeigniter pagination, Making Simple Pagination with CodeIgniter Framework, pagination in codeigniter for home page, simple pagination in codeigniter, codeigniter pagination example download, pagination in codeigniter step by step, codeigniter pagination bootstrap, pagination in codeigniter source code, codeigniter pagination limit offset, pagination codeigniter

Mukesh Jakhar - PHP Web Developer
Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.

I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.