#native_company# #native_desc#
#native_cta#

php pagination

By niroshan fernandez
on September 13, 2004

Version: 1

Type: Full Script

Category: Other

License: GNU General Public License

Description: pagination class. for pagin large sql result sets over an number of pages.

<?php
class Pagination
{
	var $limit;
	var $sql;

	var $mainQuery;
	var $result;
	var $total;
	var $numPages;
	var $page;
	var $offset;
	var $i;
	var $str;
	var $fullStr;
	var $pageName;
	var $noOfColumns=1;
	var $displayStr;
	var $row;

	var $seperator="|";
	var $emptyRowAfterData=0;

	var $displayHeadingBgColor;
	var $displayHeadingForeColor;
	var $displayHeadingForeSize;
	var $displayHeadingFace;
	var $displayHeadingAlign=1;
	var $displayHeadingStyleClass;

	var $displayDataStyleClass;
	var $displayDataBgColor;
	var $displayDataForeColor;
	var $displayDataForeSize;
	var $displayDataFace;

	var $displayExtraBgColor;

	var $noListStyleClass;
	var $noListBgColor;
	var $noListFontColor;
	var $noListFontFace;
	var $noListFontSize;


	var $displayNameArr = array();

	function setNoListFontColor($getNoListFontColo)	//this method is assign the fore color to the page no`s 
	{
		$this->noListFontColor=$getNoListFontColo;
	}

	function setNoListFontFace($getNoListFontFace) //this method is assign the font to the page no`s 
	{
		$this->noListFontFace=$getNoListFontFace;
	}

	function setNoListFontSize($getNoListFontSize) //this method is assign the font size to the page no`s 
	{
		$this->noListFontSize=$getNoListFontSize;
	}

	function setNoListBgColor($getNoListBgColor) //this method is assign the back ground color to the page no`s 
	{
		$this->noListBgColor=$getNoListBgColor;
	}

	function setNoListStyleClass($getNoListClass) //this method is assign the style sheet to the page no`s 
	{
		$this->noListStyleClass=$getNoListClass;
	}

	function setDisplayDataForeColor($getDisForeColor) //this method is assign the fore color to the data displayed
	{
		$this->displayDataForeColor=$getDisForeColor;
	}

	function setDisplayDataForeSize($getDisSize) //this method is assign the font size to the data displayed
	{
		$this->displayDataForeSize=$getDisSize;
	}

	function setDisplayDataFace($getDisFace) //this method is assign the font to the data displayed
	{
		$this->displayDataFace=$getDisFace;
	}

	function setDisplayDataBgColor($getDisBgColor) //this method is assign the back ground color to the data displayed
	{
		$this->displayDataBgColor=$getDisBgColor;
	}

	function setDisplayDataStyleClass($getDataClass) //this method is assign the style sheet to the data displayed
	{
		$this->displayDataStyleClass=$getDataClass;
	}

	function setDisplayExtraBgColor($getExBgColor) //this method is assign the back ground color to the extra row
	{
		$this->displayExtraBgColor=$getExBgColor;
	}
	
	function setEmptyRowAfterData($getEmpty) //if this set to 1 then extra row will be added
	{
		$this->emptyRowAfterData=$getEmpty;
	}

	function setSeperator($getSeperator) //assign the custamize separators 
	{
		$this->seperator=$getSeperator;
	}

	function setDisplayHeadingFace($getFace) //assign the heading font	
	{
		$this->displayHeadingFace=$getFace;
	}

	function setDisplayHeadingStyleClass($getClass) //assign the style sheet for the heading
	{
		$this->displayHeadingStyleClass=$getClass;
	}

	function setDisplayHeadingForeSize($getSize) //assign the heading fore size
	{
		$this->displayHeadingForeSize=$getSize;
	}

	function setDisplayHeadingForeColor($getForeColor) //assign the heading fore color
	{
		$this->displayHeadingForeColor=$getForeColor;
	}

	function setDisplayHeadingBgColor($gotColor) //assign the heading back color	
	{
		$this->displayHeadingBgColor=$gotColor;
	}

	function setDisplayHeadingAlign($gotAlign) //assign the headings alignment
	{
		$this->displayHeadingAlign=$gotAlign;
	}
	
	function setDisplayNames($gotNames) //assign the heading names	
	{
		$this->displayNameArr=$gotNames;
	}
	function setColumns($no) //assign the no of columns to be displayed
	{
		$this->noOfColumns=$no;
	}

	function setLimit($limiter) //assign the no of records per a page
	{
		$this->limit=$limiter;
	}

	function setPageName($gotName) //assign the page name
	{
		$this->pageName=$gotName;
	}

	function setQuery($sqlQuery) //assign the query
	{
		if (strpos($sqlQuery,"limit")<> 0)
		{
			die ($this->error('<b>Limit</b> is a reserved keyword. - setQuery()'));
		}
		else
		{
			$this->sql=$sqlQuery;
		}
	}

	function setPages($gotPage) //assign the current page
	{
		$this->page=$gotPage;
	}

	function error($errName) //error handler
	{
		print "<table border='0'><tr bgcolor='red'><td><font color='#fffffff'>Error</font></td></tr><tr><td>".$errName."</td></tr></table>";
	}

	function displayPages() // display all the recods
	{		
		if ($this->pageName == "") // if page name is not given this generate an error
		{
			die ($this->error('Please specify the destination page'));
		}
		if ($this->sql=="")	// if sql query is not specified this will generate an error
		{
			die ($this->error('Query haven`t specified'));
		}

		$this->displayStr="";
		$this->fullStr="";
		$this->str="";

		$this->mainQuery=$this->sql;		

		$this->result=mysql_query($this->mainQuery) or die ($this->error('Invalid query')); // execute the query
		$this->total=mysql_num_rows($this->result); // get the no of records
		
		$this->numPages = ceil($this->total / $this->limit); // get the no of pages to be displayed

		if ($this->page=="") // if pages is equal to nothing set it to the first page
		{
			$this->page=1;
		}

		if ($this->page > $this->numPages) //if some person try to enter invalid page no manually this will set page to last one
		{
			$this->page=$this->numPages;
		}

		$this->offset = ($this->page - 1) * $this->limit; // get the offset for the query

		$this->mainQuery=$this->sql."  LIMIT ".$this->offset." , ".$this->limit;
		$this->result=mysql_query($this->mainQuery) or die ($this->error('Invalid query'));
	
		//display results begin	

		$this->fullStr="<table border='0'>";
		
		//generate the headings for display - start
		if (count($this->displayNameArr)<>0)
		{
			$this->displayStr = "<tr "; 
			if ($this->displayHeadingBgColor <> "") 
			{
				$this->displayStr .= "bgcolor='".$this->displayHeadingBgColor."'";  // set the headings background color
			}

			if ($this->displayHeadingStyleClass <> "")
			{
				$this->displayStr .= "class='".$this->displayHeadingStyleClass."' "; // set the headings style sheet
			}
			
			$this->displayStr .= ">";

			for ($this->i=0;$this->i<count($this->displayNameArr);$this->i++) // for loop will run untill it reach the no of elements in 																	the heading array
			{
				$this->displayStr .= "<td><div align='";

				if ($this->displayHeadingAlign==0)							// set the headings alignment to left
				{ 
					$this->displayStr .= "left";
				}
				else if ($this->displayHeadingAlign==1)						// set the headings alignment to center
				{
					$this->displayStr .= "center";
				} 
				else if ($this->displayHeadingAlign==2)						// set the headings alignment to right
				{
					$this->displayStr .= "right";
				}
				else 
				{
					die ($this->error('Invalid argument - setDisplayHeadingAlign()')); // if invalied aligment value is given then this 																   will fire an error msg	
				}
				
				$this->displayStr .= "'>";
				
				if ($this->displayHeadingForeColor=="" && $this->displayHeadingForeSize=="" && $this->displayHeadingFace=="")
				{
					$this->displayStr .= $this->displayNameArr[$this->i]."</div></td>";	//if font propaties not set then display the 																		contents without the font tags
				}
				else
				{
					$this->displayStr .= "<font ";
					
					if ($this->displayHeadingForeColor<> "")
					{
						$this->displayStr .= "color='".$this->displayHeadingForeColor."' "; //set the font color of the heading
					}
					
					if ($this->displayHeadingForeSize <> "")
					{
						$this->displayStr .= "size='".$this->displayHeadingForeSize."' "; //set the font size of the heading
					}

					if ($this->displayHeadingFace <> "")
					{
						$this->displayStr .= "face='".$this->displayHeadingFace."' "; //set the font of the heading
					}
				
					$this->displayStr .= ">".$this->displayNameArr[$this->i]."</font></div></td>"; //display the heading from the heading 																			  array
				}			
			}

			$this->displayStr=$this->displayStr."</tr>";

			
			if(count($this->displayNameArr)<$this->noOfColumns) // if both setDisplayNames() and setColumns() called then if column array 													count didnt mach the setColumns() method argument then this error will 														fire
			{
				die($this->error('No of columns do not match - setDisplayNames() and setColumns()'));
			}

			if($this->noOfColumns==1 && count($this->displayNameArr) > 1) //this will over write the setColumns() argument and no of 																  column will assign to the count of the column array
			{
				$this->noOfColumns=count($this->displayNameArr);	
			}

		}
		//generate the headings for display - end


		while ($this->row=mysql_fetch_array($this->result))
		{	
			$this->displayStr .= "<tr";
			
			if ($this->displayDataStyleClass <> "")
			{
				$this->displayStr .= " class='".$this->displayDataStyleClass."' ";  // set the style sheet of the results
			}

			if ($this->displayDataBgColor <> "")
			{
				$this->displayStr .= " bgColor='".$this->displayDataBgColor."' "; // set the back ground color of the results
			}
			
			$this->displayStr .= ">";

			for ($this->x=0;$this->x<$this->noOfColumns;$this->x++) // create the columns in the results
			{				
				$this->displayStr .= "<td>";

				if ($this->displayDataForeColor == "" && $this->displayDataForeSize == "" && $this->displayDataFace =="")
				{
					$this->displayStr .= $this->row[$this->x]."</td>"; //if font propaties not set then display the 																		contents without the font tags
				}
				else
				{
					$this->displayStr .= "<font ";

					if ($this->displayDataForeColor <> "")
					{
						$this->displayStr .= "color='".$this->displayDataForeColor."' "; //set font color of the results
					}
					if ($this->displayDataForeSize <> "")
					{
						$this->displayStr .= "size='".$this->displayDataForeSize."' "; //set font size of the results
					}
					if ($this->displayDataFace <> "")
					{
						$this->displayStr .= "face='".$this->displayDataFace."' "; //set font of the results
					}

					$this->displayStr .= ">".$this->row[$this->x]."</td>";	//display records from the record set
				}
			}
			$this->displayStr=$this->displayStr."</tr>";

			if ($this->emptyRowAfterData==1) //add spaces between results if the methoud set to true
			{
				$this->displayStr .="<tr";
				if ($this->displayDataStyleClass <> "")
				{
					$this->displayStr .= " class='".$this->displayDataStyleClass."' "; // set the style of the extra line
				}
				if ($this->displayExtraBgColor <> "")
				{
					$this->displayStr .= " bgColor='".$this->displayExtraBgColor."' "; // set the back color of the extra line
				}
				
				$this->displayStr .= "><td colspan='".$this->noOfColumns."'>&nbsp;</td></tr>";

			}
		}
		$this->fullStr=$this->fullStr.$this->displayStr."<table>";

		//display results end

		//display pagin begin

		$this->fullStr=$this->fullStr."<table border='0'><tr><td>&nbsp;</td></tr><tr>";

		if ($this->noListStyleClass == "" && $this->noListBgColor == "") //if back grond color and style sheet is not set to the page 																	 no`s then ignore those values and create the row
		{
			$this->fullStr=$this->fullStr."<td>";
		}
		else
		{
			$this->fullStr=$this->fullStr."<td ";

			if ($this->noListStyleClass <> "")
			{
				$this->fullStr=$this->fullStr."class='".$this->noListStyleClass."' "; // set the style sheet for the page no`s
			}

			if ($this->noListBgColor <> "")
			{
				$this->fullStr=$this->fullStr."bgColor='".$this->noListBgColor."' "; // set the back ground color for the page no`s
			}

			$this->fullStr=$this->fullStr.">";	
			
		}		

		for($this->i=1;$this->i<=$this->numPages;$this->i++) // generate a no for individual pages
		{
			if($this->str=="")
			{
				if ($this->page== "" || $this->page ==1 )
				{
					$this->str=$this->i;		// if first page is selected remove the hyper link from that no
				}
				else
				{
					$this->str="<a href='".$this->pageName."?page=".$this->i."'>".$this->i."</a>"; // else add the hyper link for the no
				}				
			}
			else //generate rest of the page no`s
			{
				if ($this->page == $this->i)
				{
					$this->str .=" ".$this->seperator." ".$this->i;
				}
				else
				{
					$this->str .=" ".$this->seperator." "."<a href='".$this->pageName."?page=".$this->i."'>".$this->i."</a>";
				}				
			}	
		}		

		if ($this->noListFontColor == "" && $thia->noListFontFace == "" && $this->noListFontSize =="") //if font propaties not set then 																							  display the contents without the 																								  font tags
		{
			$this->fullStr .= $this->str."</td></tr></table>";
		}
		else
		{
			$this->fullStr .= "<font ";			

			if ($this->noListFontColor <> "")
			{
				$this->fullStr .= "color='".$this->noListFontColor."' "; //set the font color of the page no`s
			}
			if ($this->noListFontFace <> "")
			{
				$this->fullStr .= "face='".$this->noListFontFace."' "; //set the font of the page no`s
			}
			if ($this->noListFontSize <> "")
			{
				$this->fullStr .= "size='".$this->noListFontSize."' "; //set the font size of the page no`s
			}
			

			$this->fullStr .= ">".$this->str."</font></td></tr></table>";
		}		

		//display pagin end

		echo $this->fullStr; // display full results with heading and and page no`s
	}
}

?>