Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Pagination
3.
Pagination in CodeIgniter
4.
Configuration of Database 
5.
Pagination Database Model in CodeIgniter
6.
Controller for Pagination in CodeIgniter
7.
View for Pagination in CodeIgniter
8.
Frequently Asked Questions
8.1.
Why do we use a PHP framework?
8.2.
Is PHP front end or back end?
8.3.
Is PHP Hard to Learn? 
8.4.
What is better, PHP or Python?
8.5.
Should I learn PHP or NodeJS?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

Pagination class() in codeigniter

Author Amit Singh
0 upvote

Introduction

Have you ever worked in PHP, a top-notch scripting language for the server side? Have you used any PHP framework for boosting an application's performance?

topic

This article focuses on PHP's framework, CodeIgniter. We will study the Pagination in CodeIgniter in detail. We will try to understand it with a proper example. Let's see all these things in more detail.

Recommended Topic, Cognizant Eligibility Criteria

Pagination

Pagination is the process that involves dividing printed or digital content onto separate pages.

Pagination also refers to the automated procedure of adding consecutive numbers to designate the sequential arrangement of pages in print documents and some online content. The user experience of some website material, such as blogs, discussion forums, and search engine results pages (SERP), is improved when divided into individual pages.

Pagination in CodeIgniter

Pagination is an essential feature when you are working on building an application. Assume you have an extensive database in your app and want to show it to the users. In such cases, Pagination is very helpful.

It is an important library in CodeIgniter. In this article, we will see how to make a paginated output list from a MySQL database. We will see the following things.

  1. Configuration of Database,
     
  2. Controller for Pagination,
     
  3. The Pagination Library,
     
  4. Model for Pagination

Configuration of Database 

The first step will be to configure a database. We will create a database named "." Then, we will create a table and put some dummy records. We will store at least 50 records. We will display ten records per page on the View. 

We can create a table using the following command.

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `course` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
)


Now, we can insert 50 dummy records into the table. 

INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Aditi','aditi@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Ashish','ashish@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Priyanshu','priyanshu@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Sarthak','sarthakbaby@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Pranav','pranavvermachills@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Amit','shadow@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Aditi','aditi@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Ashish','ashish@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Priyanshu','priyanshu@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Sarthak','sarthakbaby@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Pranav','pranavvermachills@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Amit','shadow@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Aditi','aditi@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Ashish','ashish@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Priyanshu','priyanshu@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Sarthak','sarthakbaby@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Pranav','pranavvermachills@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Amit','shadow@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Aditi','aditi@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Ashish','ashish@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Priyanshu','priyanshu@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Sarthak','sarthakbaby@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Pranav','pranavvermachills@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Amit','shadow@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Aditi','aditi@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Ashish','ashish@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Priyanshu','priyanshu@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Sarthak','sarthakbaby@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Pranav','pranavvermachills@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Amit','shadow@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Aditi','aditi@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Ashish','ashish@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Priyanshu','priyanshu@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Sarthak','sarthakbaby@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Pranav','pranavvermachills@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Amit','shadow@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Aditi','aditi@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Ashish','ashish@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Priyanshu','priyanshu@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Sarthak','sarthakbaby@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Pranav','pranavvermachills@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Amit','shadow@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Aditi','aditi@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Ashish','ashish@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Priyanshu','priyanshu@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Sarthak','sarthakbaby@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Pranav','pranavvermachills@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Amit','shadow@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Aditi','aditi@gmail.com','CodeIgniter');
INSERT INTO `student`(`name`, `email`, `course`) VALUES ('Ashish','ashish@gmail.com','CodeIgniter');


Output:

output

Pagination Database Model in CodeIgniter

You must create a model in the application/model folder. We have named it "UserPagination_Model.php."

<?php
class UserPagination_Model extends CI_Model 
{
 // This is the Model for codeigniter pagination project.
    public function get_count() 
{
        return $this->db->count_all("user");
    }


    public function get_users($limit, $start) 
{
// This is the get users function for getting users in codeigniter pagination.
        $this->db->limit($limit, $start);
        $query = $this->db->get("user");
        return $query->result();
    }
}
?>

Controller for Pagination in CodeIgniter

Now, you need to create a Controller "UserPagination_Controller.php." You must store the file in the application/controller.

Now, you must insert the following code into the file:

<?php defined('BASEPATH') OR exit('No direct script access allowed');


class UserPagination_Controller extends CI_Controller {
// This is the Controller for codeigniter pagination project.
    public function __construct() 
{
        parent:: __construct();
// This is the construct function in the controller for the codeigniter pagination project.
        $this->load->helper('url','form');
        $this->load->library("pagination");
$this->load->model('UserPagination_Model');
    }


    public function index() 
{
// This is the index function in the controller for the codeigniter pagination project.
        $config = array();
        $config["base_url"] = base_url() . "index.php/UserPagination_Controller/index";
        $config["total_rows"] = $this->UserPagination_Model->get_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;


        $this->pagination->initialize($config);


$page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;

        $data["links"] = $this->pagination->create_links();


        $data['user'] = $this->UserPagination_Model->get_users($config["per_page"], $page);


        $this->load->view('pagination_view', $data);
    }
}
?>

Must Read PHP Projects With Source Code

View for Pagination in CodeIgniter

Finally, it's time to create a view for the output. You can save the View file in the application/views folder. We are naming it "pagination_view.php."

Now, add the following code in the "pagination_view.php" file.

<!DOCTYPE html>
<html>
    <head>
        <title>CodeIgniter Pagination Example on Coding Ninjas</title>
    </head>
    <body>
        <div class="container">
            <h2 class="text-primary">CodeIgniter Pagination Example on Coding Ninjas</h2>
               <table class=" table table-bordered">
                        <tr>
                            <th>Id</th>
                            <th>Name</th>
                            <th>Email ID</th>
                            <th>Course</th>
                        </tr>
<tbody>
                        <?php foreach ($user as $res): ?>
// This is the View for codeigniter pagination project.
// This is the table to show the data in codeigniter pagination project.
                            <tr>
                                <td><?php echo  $res->id ?></td>
                                <td><?php echo  $res->name ?></td>
                                <td><?php  echo  $res->email ?></td>
                                <td><?php echo  $res->course ?></td>
                               </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <p><?php echo $links; ?></p>
            </div>
        </div>
    </body>
</html>

 

Must Read: Laravel Facades

Frequently Asked Questions

Why do we use a PHP framework?

You can use a PHP framework to reduce development time because frameworks can automate many tasks.

Is PHP front end or back end?

PHP is a back-end language, also known as the scripting language. When any user request a PHP page, the server parses the PHP code. In most cases, it results in dynamically created HTML.

Is PHP Hard to Learn? 

No, PHP is one of the more accessible programming languages to learn. This is because PHP has a robust ecosystem of resources available for beginners and a syntax that forgives beginners.

What is better, PHP or Python?

Python is better for long-term projects. PHP is initially simple to use and has a relatively short learning curve. Python uses pretty strict indentation enforcements. This makes it more readable than PHP.

Should I learn PHP or NodeJS?

While PHP may feel faster to code, Node js provides a much better application speed. It has a non-blocking IO model that allows developers to deploy software that performs significantly quicker.

Conclusion

In this article, we have studied PHP's framework, CodeIgniter. We have studied the CodeIgniter Pagination in detail. We have explained it with an example in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding CodeIgniter and if you would like to learn more, check out our articles on yii-vs-codeigniter and crud-in-codeigniter.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass