Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Dynamic Pagination

, 22 Jan 2010
Rate this:
Please Sign up or sign in to vote.
The pagination is a tool which help you to split large result sets over multiple pages.The only thing that you must to do is to follow the next pattern:paginationGetLimit();mysql query(variable $limit...
The pagination is a tool which help you to split large result sets over multiple pages.
The only thing that you must to do is to follow the next pattern:
 
<?php
…
$paginationObj = new pagination(total_limit);
$limit = $paginationObj->paginationGetLimit();
 
mysql query(variable $limit goes here)
…
$paginationObj->paginationCreatePages();
…
?>
 
Default values
 
total_limit:500
results per page:30
Number of pagination links:5
 
Note: works only with dynamic urls.(http://www.example.com/script.php?page=num or script.php?var=something&page=num etc)
 
class pagination
 {
  
   private $max_rows = 30;//MAXIMUM NUMBER OF DISPLAYING ITEMS
   private $max_num = 5;//MAXIMUM NUMBER OF NUMERIC LINKS
   private $limit;
   private $maxId;//TOTAL NUMBER OF ITEMS
   private $lastpage;
   private $page;
   private $url;
   private $match = "page=";
 
 
 public function __construct($maxId = null)
 {
   if(!$maxId)
   {
     $this->maxId = 500;
   }
   else
   {
     $this->maxId = $maxId;
   }
 }
 
 
 public function getPage()
 {
   return $this->page;
 }
 
 
 public function paginationGetLimit()
 {
   $this->page = isset($_GET['page']) ? strip_tags($_GET['page']) : 1;
   $this->lastpage = ceil($this->maxId / $this->max_rows);
   $this->page = (int)$this->page;
 
   if($this->page < 1)
   {
     $this->page = 1;
   }
   elseif($this->page > $this->lastpage)
   {
     $this->page = $this->lastpage;
   }
 
   return ($this->limit = 'LIMIT ' .($this->page - 1) * $this->max_rows .',' .$this->max_rows);
 }
 
 
 public function paginationCreatePages()
 {
   
   $this->url = $_SERVER['REQUEST_URI'];//THE REQUESTED URL
   $pos = strpos($this->url, $this->match);
   .
   echo "<div class='pagination'>";
   echo "<ul>";
   //NEXT ENTRIES
   if ($this->page == 1)
   {
     //do nothing
   }
   else
   {
     $prevpage = $this->page-1;
 
     if($pos != '')
     {
       $nextUrl = str_replace($this->match.$this->page, $this->match.$prevpage, $this->url);
 
       echo "<li>";
       echo "<a href='".$nextUrl."'>&laquo;</a>";
       echo "</li> ";
     }
     else
     {
       print "The document can not create pages";
       return;
     }
   }
   //MIDDLE PAGES
   if($this->lastpage != 1)
   {
     $max_links = $this->max_num + 1;
     $h = 1;
 
     if($this->page > $max_links)
     {
       $h = (($h + $this->page) - $max_links);
     }
 
     if($this->page >= 1)
     {
       $max_links = $max_links + ($this->page-1);
     }
 
     if($max_links > $this->lastpage)
     {
       $max_links = $this->lastpage + 1;
     }
 
     for($i=$h; $i<$max_links; $i++)
     {
       if ($i == $this->page)
       {
         echo "<li id='f'>";
         echo $i;
         echo "</li>";
       }
       else
       {
         if($pos == '')
         {
           if(strpos($this->url, '?') != '')
           {
             $specialChar = "&amp;";
           }
           else
           {
             $specialChar = "?";
           }
 
           $currentUrl = $this->url.$specialChar.$this->match.$i;
         }
         else
         {
           $currentUrl = str_replace($this->match.$this->page, $this->match.$i, $this->url);
         }
 
         echo "<li>";
         echo "<a href='".$currentUrl."'>$i</a>";
         echo "</li> ";
       }
     }
   }
   //PREVIOUS ENTRIES
   if ($this->page == $this->lastpage)
   {
     //do nothing
   }
   else
   {
     $nextpage = $this->page + 1;
 
     if($pos == '')
     {
       if(strpos($this->url, '?') != '')
       {
         $specialChar = "&amp;";
       }
       else
       {
         $specialChar = "?";
       }
 
       $prevUrl = $this->url.$specialChar.$this->match.$nextpage;
     }
     else
     {
       $prevUrl = str_replace($this->match.$this->page, $this->match.$nextpage, $this->url);
     }
 
     echo "<li>";
     echo "<a href='".$prevUrl."'>&raquo;</a>";
     echo "</li> ";
   }
   
   echo "</ul>";
   echo "</div>";
 }
 
 
}
 
CSS
 
.pagination
 {
   width:100%;
   margin-top:20px;
   margin-left:10px;
   clear:left
 }
 
 .pagination ul
 {
   list-style-type: none;
   margin:0;
   padding:0;
 }
 
 .pagination ul li
 {
   color:#666666;
   float:left;
   font: Eras Bold ITC;
   font-size: 12px;
   letter-spacing: .01em;
 }
 
 .pagination ul li a
 {
   color: #47809E;
   display: block;
   margin: 0 0.1em;
   padding: 2px;
   padding-left: 4px;
   padding-right: 4px;
   text-decoration: none;
 }
 
 li#f
 {
   background-color:#fff;
   display: block;
   margin: 0 0.1em;
   padding: 2px;
   padding-left: 4px;
   padding-right: 4px;
   text-decoration: none;
   color:#666666;
 }
 
 .pagination ul li a:hover
 {
   text-decoration:underline;
 }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

caguras

Albania Albania
No Biography provided

Comments and Discussions

 
QuestionVery smart! PinmemberS.M. Zamshed Farhan12-Jan-13 3:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 22 Jan 2010
Article Copyright 2010 by caguras
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid