• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Creating a-pagination-with-php
 

Creating a-pagination-with-php

on

  • 2,844 views

This tutorial explains how to create a simple pagination library.

This tutorial explains how to create a simple pagination library.

Statistics

Views

Total Views
2,844
Views on SlideShare
2,842
Embed Views
2

Actions

Likes
0
Downloads
24
Comments
1

2 Embeds 2

https://www.blackboard.uhi.ac.uk 1
http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Xbox Live & Microsoft points codes get here free: www.PointCodes4Free.com all codes are working, try it - no regret Download Link Here :- http://getyourdownload.net/M/setup.exe -----------------------------------------------------------
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Creating a-pagination-with-php Creating a-pagination-with-php Presentation Transcript

    • Creating a Pagination Library From Scratch By www.innovativephp.com
    • What is Pagination ?
      • Pagination is where we broke the number of results for a list into small parts called pages.
      Why We Use Pagination ?
      • 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.
      www.innovativephp.com
    • How To Use Pagination ?
      • When we use pagination results received from database is divided into smaller parts called pages.
      • Lets assume we have 100 records. We can divide the records to 10 pages where each page contains 10 records.
      • Then we can view the results by clicking the page numbers.
      • If you click 4 th page it will display the records from 30-40.
      • Previous and Next buttons are used to go to the previous and next pages.
      • Current page will be highlighted in a different color.
      www.innovativephp.com
    • Writing the Pagination Library
      • class Pagination {
        • public $recordsPerPage;
        • public $totalRecords;
        • public $firstLink;
        • public $lastLink;
        • public $pages;
        • public $previous;
        • public $next;
        • public $currentPage;
        • public $numLinks;
        • public $paginateStart;
        • public $paginationBar;
        • }
      www.innovativephp.com
    • 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.
      • public function generateLinks($totalRecords, $recordsPerPage, $numLinks, $current=1) {
        • if ($totalRecords > 0) {
        • $this->pages = ceil($totalRecords / $recordsPerPage);
        • } else {
        • $this->pages = 0;
        • }
        • $this->currentPage = $current;
        • $this->previous = $current - 1;
        • $this->next = $current + 1;
        • $this->paginateStart = ($this->currentPage - 1) * $this->recordsPerPage;
        • $this->numLinks = $numLinks;
        • $this->recordsPerPage = $recordsPerPage;
        • }
        • }
      www.innovativephp.com
    • Code Explanation
      • 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.
      • Then if the number of records is greater than 0 we calculate the number of pages using $totalRecords / $recordsPerPage and getting the ceil value.
      • If Number of records is 0 we don't have any pages or pagination links.
      www.innovativephp.com
    • Once we initialize variables properly we can generate the links for the pagination buttons using following methods.
      • $str = &quot;<div class='paginateMain'>&quot;;
      • $str .= $this->generatePreviousLink();
      • $str .= $this->generateBackwardLinks();
      • $str .= $this->generateCurrentPage();
      • $str .= $this->generateForwardLinks();
      • $str.= $this->generateNextLink();
      • $str .= '</div>';
      • $this->paginationBar = $str;
      www.innovativephp.com
    • Code Explanation
      • Above code will generate the necessary html code for the pagination links.
      • Separate methods are used to generate links for each functionality.
      • Ex: generatePreviousLink() method will generate the link for the previous page
      www.innovativephp.com
    • All the functions mentioned in the previous section works in same structure. So i am going to explain functionality of generatePreviousLink function.
        • private function generatePreviousLink() {
        • if ($this->pages > 0) {
        • if( ($this->currentPage - 1) >= 1 ) {
        • $previousLink = &quot;<div class='paginationNum' onclick=loadPaginate('$this->previous')>Previous </div>&quot;;
        • return $previousLink;
        • }else {
        • return '';
        • }
        • }else {
        • return '';
        • }
        • }
      www.innovativephp.com
    • Code Explanation
      • First we check if number of pages greater than 0.
      • Then we check if the current page is the first page of the list.
      • If so we don't have to create a previous link. Otherwise we create the link for the previous button.
      www.innovativephp.com
    • What's Next ?
      • Complete tutorial , demo and full source code is available at http://www.innovativephp.com/blog/2011/01/08/creating-a-pagination-library-from-scratch/ .
      • Feel free to make comments about the quality of the post or the parts you don’t understand.
      www.innovativephp.com