Contents
- Download Codeigniter 3
- Create a new Database
- Create a new table
- Create a new project name
- Create a new controller file
- Load autoload data
- Load pagination library
- Load helper class
- Create a new model file
- Create a new view file
Download CodeIgniter
The first step in setting up CodeIgniter is to download it from the official website. You can download the latest version of CodeIgniter 3 from the following URL:
https://codeigniter.com/userguide3/installation/downloads.html
What is CodeIgniter Pagination Library?
Following these steps:
- The database name is sample
- Create a new table in the Mysql database
- The project folder name is codeigniter
- Create a new controller file in Path: codeigniter\application\controllers\Home.php
- Create a new view file in Path: codeigniter\application\views\home_view.php
- Create a new model file in Path: codeigniter\application\models\Home_model.php
For creating a table the SQL query is:
CREATE TABLE crud (`id` int(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,`first_name` varchar(30) NOT NULL,`last_name` varchar(30) NOT NULL,`email` varchar(30) NOT NULL,`mobile` varchar(30) NOT NULL);
Load libraries and helper
- Open the "application/config/autoload.php" file in your CodeIgniter 3 project.
- Find the line that reads "$autoload['libraries'] = array();" and add the name of the library you want to autoload inside the array.
$autoload['libraries'] = array('database', 'pagination');
To autoload a helper in CodeIgniter 3, you need to follow these steps:
- Open the "application/config/autoload.php" file in your CodeIgniter 3 project.
- Find the line that reads "$autoload['helper'] = array();" and add the name of the helper you want to autoload inside the array.
$autoload['helper'] = array('url');
Create new controller
<?phpdefined('BASEPATH') OR exit('No direct script access allowed');class Home extends CI_Controller {public function __construct(){/*call CodeIgniter's default Constructor*/parent::__construct();/*Load Model*/$this->load->model('home_model');}public function index($rowno=0){$rowperpage = 5;if($rowno != 0){$rowno = ($rowno-1) * $rowperpage;}// Select total records count from database using this model function$allcount = $this->home_model->getrecordCount();$config['base_url'] = base_url().'index.php/home/index';$config['use_page_numbers'] = TRUE;$config['total_rows'] = $allcount;$config['per_page'] = $rowperpage;// Initialize$this->pagination->initialize($config);// Pagination link generate$data['pagination'] = $this->pagination->create_links();// Fetch total records from database using this model function$data['results'] = $this->home_model->getData($rowno,$rowperpage);$data['row'] = $rowno;$this->load->view('home_view',$data);}} ?>
Create new model
<?php class Home_model extends CI_Model{public function __construct() {parent::__construct();}// Select total records count from databasepublic function getrecordCount(){$this->db->select('count(*) as allcount');$this->db->from('crud');$query = $this->db->get();$result = $query->result_array();return $result[0]['allcount'];}// Fetch total records from databasepublic function getData($rowno,$rowperpage){$this->db->select('*');$this->db->from('crud');$this->db->limit($rowperpage, $rowno);$query = $this->db->get();return $query->result_array();}}?>
Create new view
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script><meta charset="utf-8"><style type="text/css">::selection {background-color: #E13300;color: white;}::-moz-selection {background-color: #E13300;color: white;}body {background-color: #fff;margin: 40px;font: 13px/20px normal Helvetica, Arial, sans-serif;color: #4F5155;}a {color: #003399;background-color: transparent;font-weight: normal;text-decoration: none;}a:hover {color: #97310e;}h1 {color: #444;background-color: transparent;border-bottom: 1px solid #D0D0D0;font-size: 19px;font-weight: normal;margin: 0 0 14px 0;padding: 14px 15px 10px 15px;}code {font-family: Consolas, Monaco, Courier New, Courier, monospace;font-size: 12px;background-color: #f9f9f9;border: 1px solid #D0D0D0;color: #002166;display: block;margin: 14px 0 14px 0;padding: 12px 10px 12px 10px;}#body {margin: 0 15px 0 15px;min-height: 96px;}p {margin: 0 0 10px;padding: 0;}p.footer {text-align: right;font-size: 11px;border-top: 1px solid #D0D0D0;line-height: 32px;padding: 0 10px 0 10px;margin: 20px 0 0 0;}#container {margin: 10px;border: 1px solid #D0D0D0;box-shadow: 0 0 8px #D0D0D0;}</style></head><body><div class="container mt-3"><h2>Pagination in Codeigniter 3</h2><table class="table table-bordered"><thead><tr><th>Firstname</th><th>Lastname</th><th>Email</th><th>Mobile Number</th></tr></thead><tbody><?php if (!empty($results)) {foreach ($results as $row) { ?><tr><td><?php echo $row["first_name"]; ?></td><td><?php echo $row["last_name"]; ?></td><td><?php echo $row["email"]; ?></td><td><?php echo $row["mobile"]; ?></td></tr><?php }} else { ?><p>Post(s) not found...</p><?php } ?></tbody></table><div class="pagination"><?php echo $this->pagination->create_links(); ?></div></div></body></html>
<?php echo $this->pagination->create_links(); ?>
This code will create a pagination links at the bottom of the page that allows users to navigate through the data.
Finally enter your project url in browser : "http://localhost/codeigniter/index.php/home/index"
Output
Conclusion
CodeIgniter Pagination Library is a powerful feature that allows developers to manage large sets of data easily.
We hope this guide has been helpful. If you have any questions or need further assistance, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 Comments