CodeSamplez.com

Programming, Web development, Cloud Technologies

  • Facebook
  • Google+
  • RSS
  • Twitter
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact
Home Development How To Work With CodeIgniter Pagination

How To Work With CodeIgniter Pagination

Rana Ahsan May 22, 2013 12 Comments


 How To Work With CodeIgniter Pagination    

Pagination is one of the most frequently used features for web applications. Wherever we have a bunch of data and need to show them as a list, we require pagination to prevent hundreds/thousands of data in a single page. As a robust framework, codeigniter provides very efficient way to handle it with its integrated support. In this tutorial, we will see, how we can implement codeigniter pagination and enhance it further as per our need.

I am assuming you have already have basic experience of working with codeigniter framework before. Also, I will be sharing only partial code snippet related to pagination functionality. If you are looking for the complete code reference. You can find it on demo example page.


Checkout The CodeIngiter Pagination Demo!

Initialization:

As per codeigniter’s internal structure, you can add a new php page named ‘pagination.php’ in ‘application/config’ directory and set all initialization parameters like entry per page, whether to use page number or not etc there. However, in such case, if you have some different configuration based of section, like uri segment of page number, then you will need to overwrite the existing config with them on controller function. On the other hand, if your application has more dynamic values on different sections, then it will be good idea to keep the initialization on your own helper or library function and pass necessary parameters from different section. This is what I like to use. Below is a simple codeigniter pagination initialization function:

 public function init_pagination($uri,$total_rows,$per_page=10,$segment=4){
        $ci                          =& get_instance();
        $config['per_page']          = $per_page;
        $config['uri_segment']       = $segment;
        $config['base_url']          = base_url().$uri;
        $config['total_rows']        = $total_rows;
        $config['use_page_numbers']  = TRUE;

        $ci->pagination->initialize($config);
        return $config;    
    } 

as you can see above, all keys inside ‘config’ associative array are defined by codeigniter and we need to pass necessary values for them. Brief on each item are given below:

  • ‘per_page’: Refers to the number of how many entries will be shown on each page.
  • base_url: Refers to the paginated url base.
  • total_rows: Total numer of entries in database.
  • use_page_numbers: Refers whether we want to use page number(1,2,3..) in the url or entry id(1,10, 20…) on uri segment.


Read The Complete CodeIgniter Tutorials Series By CodeSamplez.com

Retrieve Data Range From Database:

I am using a simple contact form database table for this tutorial:
simple mysql contact table structure
If you are a regular reader of my blog, you may know that I normally use doctrime ORM in codeigniter
. With use of doctrine model plugin for codeigniter, it became a lot more easier to retrieve the paginated data, as the php code example below:

$contact_messages = $this->messagemodel->get_by_range((($page-1) * $pagingConfig['per_page']),$pagingConfig['per_page']);

where ‘messagemodel’ is a simple codeigniter model extending the doctrine CRUD model as below:

class Messagemodel extends My_DModel{
    function __construct() {
        parent::__construct();
        $this->init("PdMessage",$this->doctrine->em);
    }
}

If you are not comfortable using doctrine, you can use the following model function to retrieve data using codeigniter active record class:

function get_by_range(){
    $query = $this->db->get('PdMessages', $pagingConfig['per_page'], (($page-1) * $pagingConfig['per_page']);
    return $query->result();
}

Showing Paginated Links On View:

To show pagination links, it is fairly easy to use. After you show current page’s data, just use a simple function as below:

$pagination_helper->create_links();

Integrate Twitter Bootstrap Styles With Codeigniter Pagination Links:

well, you will may always want to customize the generated links to add some beautiful styles. As twitter bootstrap being the most popular front-end css framework now a days, here is the code snippet to integrate its style to our pagination. Set the following config items before initialization of pagination library:

$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

Also, in view, use the code as follows:

<div class="pagination">
    <ul>
        {$pagination_helper->create_links()}
    </ul>    
</div>

And you should be fine with bootstrap styled pagination links 🙂 .


Checkout The CodeIngiter Pagination Demo!

Final Words:

Hope this simple codeigniter pagination tutorial will help you in some aspect in the way to your codeigniter application development. Don’t hesitate to ask me if you are have any question or issue in understanding the tutorial. Happy coding 🙂

Related

Filed Under: Development Tagged With: codeigniter, php

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Comments

  1. kago says

    July 17, 2013 at 8:15 am

    So, can you create multiple pagination links within one view?

    Reply
  2. sam says

    January 2, 2014 at 4:33 am

    source script?

    Reply
  3. Nisha says

    May 16, 2014 at 5:23 am

    Your demo of Pagination is correct as i have need of this but the code you explained is not clear to me.

    Reply
  4. Kiran Aghor says

    May 22, 2014 at 9:16 pm

    Thank you very much for bootstrap based pagination links.

    Reply
  5. prithvi pal says

    June 5, 2014 at 5:09 am

    great its truly help full.

    Reply
  6. sandeep says

    August 18, 2014 at 6:23 am

    whats about the load language[‘message’]..i cant get it

    Reply
    • Md Ali Ahsan Rana says

      August 18, 2014 at 10:32 pm

      Its used to load language file. If there isn’t, you can ignore it safely. Read the official user guide for language file

      Reply
  7. roshan dallakoti says

    November 13, 2014 at 5:16 am

    i didnot get any pagination links despite pagination library loaded and create_links function called .
    I dunno why ?

    Reply
  8. Rutvi says

    November 17, 2014 at 4:07 am

    Can you please help us to add per page selection in above example?

    Reply
  9. Rajneesh Kumar Gobin says

    March 3, 2015 at 7:44 am

    {$pagination_helper->create_links()} should not be in view, instead put the creeate link in controler like this $data[“pagination_helper”] = $this->pagination->create_links();
    then in view

    Reply
  10. ayesha zlavia says

    December 10, 2015 at 5:26 am

    its really good artical . i have read complete code igniter artical. it was very helpfull.

    Reply
    • Md Ali Ahsan Rana says

      December 18, 2015 at 1:54 pm

      Thank’s for reading all the articles! Glad to hear that you liked them!

      Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Email Subscription

Never miss any programming tutorial again.

Popular Tutorials

  • PHP HTML5 Video Streaming Tutorial
  • How To Work With JSON In Node.js / JavaScript
  • Generate HTTP Requests using c#
  • How To Work With C# Serial Port Communication
  • Facebook C# API Tutorials
  • How To Work With CodeIgniter Pagination
  • Get Facebook C# Api Access Token
  • LinQ Query With Like Operator
  • How To Work With Multithreaded Programming In C#.NET Application
  • Getting Started With HTML5 Web Speech API

Recent Tutorials

  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read
  • Getting Started With Golang Unit Testing
  • Getting Started With Big Data Analytics Pipeline
  • NodeJS Tips And Tricks For Beginners
  • Apple Push Notification Backend In NodeJS
  • Web Based Universal Language Translator, Voice/Text Messaging App
  • How To Dockerize A Multi-Container App From Scratch

Recent Comments

  • intolap on PHP HTML5 Video Streaming Tutorial
  • manishpanchal on PHP HTML5 Video Streaming Tutorial
  • Rana Ghosh on PHP HTML5 Video Streaming Tutorial
  • ld13 on Pipe Email To PHP And Parse Content
  • Daniel on PHP HTML5 Video Streaming Tutorial

Archives

Resources

  • CodeSamplez.com Demo

Tags

.net apache api audio aws c# cache cloud server codeigniter deployment doctrine facebook git github golang htaccess html5 http image java javascript linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty ssh tfs thread tips ubuntu unit-test utility web application wordpress wpf

Copyright © 2010 - 2021 · CodeSamplez.com ·

Copyright © 2021 · Streamline Pro Theme on Genesis Framework · WordPress · Log in