Jump to content

paginator shows page links even there is no data


Andy_Kemp

Recommended Posts

Paginator shows page links even there is no data.
30li5op.png
 

Paginator Class

<?php

// ==================================================================
	//  Author: Ted Kappes (pesoto74@soltec.net)
	//	Web: 	http://tkap.org/paginator/
	//	Name: 	Paginator
	// 	Desc: 	Class to help make pagination more easy.
	//
	// 7/21/2003
	//
	//  Please send me a mail telling me what you think of Paginator
	//  and what your using it for. [ pesoto74@soltec.net]
	//
// ==================================================================
class Paginator {
				//all variables are pivate.
					var $previous;	
					var $current;
					var $next;
					var $page;
					var $total_pages;
					var $link_arr;
					var $range1;
					var $range2;
					var $num_rows;
					var $first;
					var $last;
					var $first_of;
					var $second_of;
					var $limit;
					var $prev_next;
					var $base_page_num;
					var $extra_page_num;
					var $total_items;
					var $pagename;
			//Constructor for Paginator.  Takes the current page and the number of items
			//in the source data and sets the current page ($this->page) and the total
			//items in the source ($this->total_items).
			function Paginator($page,$num_rows) 
			{ 
			    if(!$page)
					{
			    $this->page=1;
					} else {
				  $this->page=$page;
				  }
				  $this->num_rows=$num_rows;
					$this->total_items = $this->num_rows;
			}
			//Takes  $limit and sets $this->limit. Calls private mehods
			//setBasePage() and setExtraPage() which use $this->limit.
			function set_Limit($limit=5)
			{
			    $this->limit = $limit;
					$this->setBasePage();
					$this->setExtraPage();
			}
			//This method creates a number that setExtraPage() uses to if there are
			//and extra pages after limit has divided the total number of pages.
			function setBasePage()
			{
			    $div=$this->num_rows/$this->limit;	
				  $this->base_page_num=floor($div);
			}
			function setExtraPage()
			{
				  $this->extra_page_num=$this->num_rows - ($this->base_page_num*$this->limit);
			}
			//Used in making numbered links.  Sets the number of links behind and 
			//ahead of the current page.  For example if there were a possiblity of
			//20 numbered links and this was set to 5 and the current link was 10
			//the result would be this 5 6 7 8 9 10 11 12 13 14 15.
			
			function set_Links($prev_next=5)
			{
			    $this->prev_next = $prev_next;
			}
			//method to get the total items.
			function getTotalItems()
			{
			$this->total_items = $this->num_rows;
			return $this->total_items;
			}
			//method to get the base number to use in queries and such.
			function getRange1()
			{
			    $this->range1=($this->limit*$this->page)-$this->limit;	
			    return $this->range1;
			}
			//method to get the offset.
			function getRange2()
			{
			    if($this->page==$this->base_page_num + 1)
 	        {
	        $this->range2=$this->extra_page_num;
				  } else { $this->range2=$this->limit;
					}
				  return $this->range2;
			}
			//method to get the first of number as in 5 of .
			function getFirstOf()
			{
			    $this->first_of=$this->range1 + 1;
			    return $this->first_of;
			}
			//method to get the second number in a series as in 5 of 8.
			function getSecondOf()
			{
			    if($this->page==$this->base_page_num + 1)
 	        {
				  $this->second_of=$this->range1 + $this->extra_page_num;
				  } else { $this->second_of=$this->range1 + $this->limit;
					       }
				  return $this->second_of;
			}
			//method to get the total number of pages.
			function getTotalPages()
			{
			    if($this->extra_page_num)
					{
					$this->total_pages = $this->base_page_num + 1;
					} else {
				  $this->total_pages = $this->base_page_num;
					       }
					return $this->total_pages;
			}
			//method to get the first link number.
			function getFirst()
			{
			    $this->first=1;
			    return $this->first;
			}
			//method to get the last link number.
			function getLast()
			{
			    if($this->page == $this->total_pages)
					{
					$this->last=0;
					}else { $this->last = $this->total_pages;
					      }
					return $this->last;  
			}
			function getPrevious()
			{
			    if($this->page > 1)
	        {
	        $this->previous = $this->page - 1;
	        }
					return $this->previous;
			}
			//method to get the number of the link previous to the current link.
			function getCurrent()
			{
			    $this->current = $this->page;
					return $this->current;
			}
			//method to get the current page name. Is mostly used in links to the next 
			//page.
			function getPageName()
			{
			    $this->pagename = $_SERVER['PHP_SELF'];;
					return $this->pagename;
			}
			//method to get the number of the link after the current link.
			function getNext()
			{   
			    $this->getTotalPages();
			    if($this->total_pages != $this->page)
				  {
				  $this->next = $this->page + 1;
				  }
					return $this->next;
			}
			//method that returns an array of the numbered links that should be 
			//displayed.   
			function getLinkArr()
      {
       //gets the top range   
       $top = $this->getTotalPages()- $this->getCurrent();
       if($top <= $this->prev_next)
         {
         $top = $top;
	       $top_range = $this->getCurrent() + $top;
	       } else { $top = $this->prev_next; $top_range = $this->getCurrent() + $top; }
				 
				//gets the bottom range
	     $bottom = $this->getCurrent() -1;
       if($bottom <= $this->prev_next)
	       {
	       $bottom = $bottom;
	       $bottom_range = $this->getCurrent() - $bottom;
	       } else { $bottom = $this->prev_next; $bottom_range = $this->getCurrent() - $bottom; } 
	 
	       $j=0;
       foreach(range($bottom_range, $top_range) as $i)
	       {
	       $this->link_arr[$j] = $i;
		     $j++;
		     }
		   return $this->link_arr;
      }
			
	}//ends Paginator class
	?>	

Paginator html

<?php 
// ==================================================================
	//  Author: Ted Kappes (pesoto74@soltec.net)
	//	Web: 	http://tkap.org/paginator/
	//	Name: 	Paginator_html
	// 	Desc: 	Class extension for Paginator. Adds pre-made link sets.
	//
	// 7/21/2003
	//
	//  Please send me a mail telling me what you think of Paginator
	//  and what your using it for. [ pesoto74@soltec.net]
	//
// ==================================================================
      
			class Paginator_html extends Paginator { 
			
			  //outputs a link set like this 1 of 4 of 25 First | Prev | Next | Last |              
				function firstLast()
			  {				
					 if($this->getCurrent()==1)
		         {
		         $first = "First | ";
		         } else { $first="<a href=\"" .  $this->getPageName() . "?page=" . $this->getFirst() . "\">First</a> |"; }  
		       if($this->getPrevious())
		         {
		         $prev = "<a href=\"" .  $this->getPageName() . "?page=" . $this->getPrevious() . "\">Prev</a> | ";
		         } else { $prev="Prev | "; }
		
	         if($this->getNext())
		         {
		         $next = "<a href=\"" . $this->getPageName() . "?page=" . $this->getNext() . "\">Next</a> | ";
		         } else { $next="Next | "; }
		
		
		       if($this->getLast())
		         {
		         $last = "<a href=\"" . $this->getPageName() . "?page=" . $this->getLast() . "\">Last</a> | ";
		         } else { $last="Last | "; }
		         echo $this->getFirstOf() . " of " .$this->getSecondOf() . " of " . $this->getTotalItems() . " ";
		         echo $first . " " . $prev . " " . $next . " " . $last;
				} 
				//outputs a link set like this Previous 1 2 3 4 5 6 Next   
				function previousNext()
				{
					if($this->getPrevious())
		        {
		        echo "<a href=\"" . $this->getPageName() . "?page=" . $this->getPrevious() . "\">Previous</a> ";
		        }
						$links = $this->getLinkArr();
		      foreach($links as $link)
	          {
	          if($link == $this->getCurrent())
					    {
					     echo " $link ";
					    } else { echo "<a href=\"" . $this->getPageName() . "?page=$link\">" . $link . "</a> ";
					    }
		          } 
						if($this->getNext())
		          {
		          echo "<a href=\"" . $this->getPageName() . "?page=" . $this->getNext() . "\">Next</a> ";
		          }
		        }  
	}//ends class


         ?>
				 

Example page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Example 2</title>
<style type="text/css">
<!-- 
body { margin-top: 20px;
			 margin-left: 150px;
			 margin-right: 150px;
			 background-color: #FFFF99; 
			 font-family: Verdana;
 } 
h1 { font-size: 150%;
 }
-->
</style>
</head>
<center>
<body>
<h1>Example 2</h1>
<?php
  //=================================================================================
	//This shows how to set things up to get your input from an array.  This could be
	//used to make an image gallery or to display anything else you might store in
	//an array. This page uses  Paginator_html for the pre-made links.  The results
	//are pretty basic.  You should be able to use something like CSS to customize these
	//for you own site. This page uses the previousNext() method. 
	//==================================================================================
  //include the main class
	include("include/paginator.php");
	//include the extension that makes the pre-made links
	include("include/paginator_html.php");
	//Makes the array used in this example.
		for($i=0; $i < 0; $i++)
			{
			$p=$i+1;
			$pictures[$i]="pict" . $p . ".jpg";
			}
		//gets the total number of items
    $num_rows = count($pictures);
		//========================================================================
		//Parts used to make a new paginator 
		//========================================================================
		//Makes new Paginator_html.  Current page here is sent by the get method. 
		//$num_rows is the total items in the source.
    $a =& new Paginator_html($_GET['page'],$num_rows);
    //sets the number of records displayed
		//defaults to five
		$a->set_Limit(4); 
		// if using numbered links this will set the number before and behind 
		//the current page.
		//defaults to five
		$a->set_Links(3);  
		//gets starting point.
		$limit1 = $a->getRange1();  
		//gets number of items displayed on page.
		$limit2 = $a->getRange2();  
		//=========================================================================
		//Printing out the items in the array
		for($j=$limit1; $j < $limit1 + $limit2; $j++)
			{
			echo "<strong>" . $pictures[$j] . "</strong></p>";
			}
		//=========================================================
		//Put this where you want your links to appear
			$a->previousNext();
		//=========================================================
			//uncomment for some info that may be helpful in debugging.
			//echo '<pre>'; print_r($a); echo '</pre>';
?>
</center>
<p>This is an example of one of the pre-made set of links. It uses the Paginator
class and the Paginator_html extension class. I use an array here to show that you 
can use this class with sources other then a database. Also I did this so you could
get an idea of how this works without having to go to the trouble of setting up a
database. This output is fairly plain which should make it easier to use something like
CSS to fit it into the look of your own site.
</p>
<p>
Check the source code of this page to see how to set something like this up.</p>
<a href="index.php">Back to Index</a>
</body>
</html>

Url For paginator files http://www.phpclasses.org/package/1239-PHP-Spliting-database-query-result-sets-between-pages-.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.