Creating a-pagination-with-php


Published on

This tutorial explains how to create a simple pagination library.

Published in: Technology
1 Comment
  • Xbox Live & Microsoft points codes get here free: all codes are working, try it - no regret Download Link Here :- -----------------------------------------------------------
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Creating a-pagination-with-php

  1. 1. Creating a Pagination Library From Scratch By
  2. 2. What is Pagination ? <ul><li>Pagination is where we broke the number of results for a list into small parts called pages. </li></ul>Why We Use Pagination ? <ul><li>Without pagination we will have to get all the results and display in a single page . It takes lot of load time and also page scroll gets very long. </li></ul>
  3. 3. How To Use Pagination ? <ul><li>When we use pagination results received from database is divided into smaller parts called pages. </li></ul><ul><li>Lets assume we have 100 records. We can divide the records to 10 pages where each page contains 10 records. </li></ul><ul><li>Then we can view the results by clicking the page numbers. </li></ul><ul><li>If you click 4 th page it will display the records from 30-40. </li></ul><ul><li>Previous and Next buttons are used to go to the previous and next pages. </li></ul><ul><li>Current page will be highlighted in a different color. </li></ul>
  4. 4. Writing the Pagination Library <ul><li>class Pagination { </li></ul><ul><ul><li>public $recordsPerPage; </li></ul></ul><ul><ul><li>public $totalRecords; </li></ul></ul><ul><ul><li>public $firstLink; </li></ul></ul><ul><ul><li>public $lastLink; </li></ul></ul><ul><ul><li>public $pages; </li></ul></ul><ul><ul><li>public $previous; </li></ul></ul><ul><ul><li>public $next; </li></ul></ul><ul><ul><li>public $currentPage; </li></ul></ul><ul><ul><li>public $numLinks; </li></ul></ul><ul><ul><li>public $paginateStart; </li></ul></ul><ul><ul><li>public $paginationBar; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  5. 5. Then we have to initialize the variables and do the calculations before preparing the Pagination Links. I have done this inside the generateLinks method in the Pagination class. <ul><li>public function generateLinks($totalRecords, $recordsPerPage, $numLinks, $current=1) { </li></ul><ul><ul><li> if ($totalRecords > 0) { </li></ul></ul><ul><ul><li>$this->pages = ceil($totalRecords / $recordsPerPage); </li></ul></ul><ul><ul><li>} else { </li></ul></ul><ul><ul><li>$this->pages = 0; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>$this->currentPage = $current; </li></ul></ul><ul><ul><li>$this->previous = $current - 1; </li></ul></ul><ul><ul><li>$this->next = $current + 1; </li></ul></ul><ul><ul><li>$this->paginateStart = ($this->currentPage - 1) * $this->recordsPerPage; </li></ul></ul><ul><ul><li>$this->numLinks = $numLinks; </li></ul></ul><ul><ul><li>$this->recordsPerPage = $recordsPerPage; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  6. 6. Code Explanation <ul><li>We have to pass the values for number of records returned from sql query , records per page , number of pages before and after current page and current page number to the generateLinks function. </li></ul><ul><li>Then if the number of records is greater than 0 we calculate the number of pages using $totalRecords / $recordsPerPage and getting the ceil value. </li></ul><ul><li>If Number of records is 0 we don't have any pages or pagination links. </li></ul>
  7. 7. Once we initialize variables properly we can generate the links for the pagination buttons using following methods. <ul><li>$str = &quot;<div class='paginateMain'>&quot;; </li></ul><ul><li>$str .= $this->generatePreviousLink(); </li></ul><ul><li>$str .= $this->generateBackwardLinks(); </li></ul><ul><li>$str .= $this->generateCurrentPage(); </li></ul><ul><li>$str .= $this->generateForwardLinks(); </li></ul><ul><li>$str.= $this->generateNextLink(); </li></ul><ul><li>$str .= '</div>'; </li></ul><ul><li>$this->paginationBar = $str; </li></ul>
  8. 8. Code Explanation <ul><li>Above code will generate the necessary html code for the pagination links. </li></ul><ul><li>Separate methods are used to generate links for each functionality. </li></ul><ul><li>Ex: generatePreviousLink() method will generate the link for the previous page </li></ul>
  9. 9. All the functions mentioned in the previous section works in same structure. So i am going to explain functionality of generatePreviousLink function. <ul><ul><li>private function generatePreviousLink() { </li></ul></ul><ul><ul><li>if ($this->pages > 0) { </li></ul></ul><ul><ul><li>if( ($this->currentPage - 1) >= 1 ) { </li></ul></ul><ul><ul><li>$previousLink = &quot;<div class='paginationNum' onclick=loadPaginate('$this->previous')>Previous </div>&quot;; </li></ul></ul><ul><ul><li>return $previousLink; </li></ul></ul><ul><ul><li>}else { </li></ul></ul><ul><ul><li>return ''; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>}else { </li></ul></ul><ul><ul><li>return ''; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  10. 10. Code Explanation <ul><li>First we check if number of pages greater than 0. </li></ul><ul><li>Then we check if the current page is the first page of the list. </li></ul><ul><li>If so we don't have to create a previous link. Otherwise we create the link for the previous button. </li></ul>
  11. 11. What's Next ? <ul><li>Complete tutorial , demo and full source code is available at . </li></ul><ul><li>Feel free to make comments about the quality of the post or the parts you don’t understand. </li></ul>