Location: PHPKode > scripts > Page nav class > page-nav-class/pagenav.php
<?php
/**
*Class that could creating page nav while using pages that lists rows from a database table.
*Needs attached db_class.php,make sure you include that file in scripts that use this class
*
Sample

$lps_page_nav_index=count($lps_page_nav);//if $lps_page_navis not already set $lps_page_nav_index will be set to 0
$lps_page_nav[]=new pagenav("pn_".$lps_page_nav_index);//the query variables will be starting with the string pn_0(for pagenavs for first lisitng in a page)
$lps_page_nav[$lps_page_nav_index]->options[rows_per_page]=10;//fetch 10 records for the given query for each page and create pagenave accordingly

$sql = "SELECT * FROM table";

$templates = $lps_page_nav[$lps_page_nav_index]->fetch_records($sql);//returns the records as an array

 echo $lps_page_nav[$lps_page_nav_index]->page_nav."<br />"//displays like 1,2,3,4
 echo $lps_page_nav[$lps_page_nav_index]->display_rec_nums."<br />";//displays like 1 to 10 of 25
 
 foreach($templates as $row_templates) 
  write code to display each record items here
}
 
* @package database and pagenav class
* @author Sreekanth Dayanand <hide@address.com>
* @version 1.0 <2009/09/18>
* @copyright GNU General Public License (GPL)
**/
class pagenav
{
 var $varname="pn";
 var $rs=array();
 var $options=array("pagelinksperpage"=>20,"rows_per_page"=>5,"class"=>"");
 var $row_count=0;
 var $page_nav="";
 var $display_rec_nums="";
 function pagenav($varname,$options=array())
 {
  $this->varname=$varname;
 }//function pagenav($varname)
 function fetch_records($sql)
 {
  global $db;//$db is an instace of MYSQL class in the attacjhed db_class.php
  $count_sql=preg_replace("/select (.+) from (.+)/i","select count(*) as tot from $2",$sql);
  $count_rs = $db->select_sql($count_sql);
  $this->row_count = $count_rs[0][tot];
  $dd_total_rows_var_name=$this->varname."_tot";
  $dd_per_page_var_name=$this->varname."_per_page";
  $dd_page_num_var_name=$this->varname."_page_num";
  $this->page_nav=$this->create_pagenav($this->row_count,$dd_total_rows_var_name,$dd_per_page_var_name,$dd_page_num_var_name,$this->options["class"]);
  $this->display_rec_nums=$this->get_display_row_limit($dd_per_page_var_name,$dd_page_num_var_name).$this->row_count; 		  
  $sql.=" ".get_row_limit($dd_per_page_var_name,$dd_page_num_var_name); 
  //echo $sql."<br />";
  $this->rs=$db->select_sql($sql);  
  return $this->rs;
		 
 }//function fetchrecords($sql)
 function first_recno_for_pageno($numrows_var,$rows_per_page_var,$nav_var)
 {
  return ($_GET[$nav_var]*$_GET[$rows_per_page_var]);
 }
 function create_pagenav($tot_rows,$numrows_var,$rows_per_page_var,$nav_var,$class="")
 {
  
         //set defaults if not declared aleady
		  if(!($_GET[$rows_per_page_var]>0)) $_GET[$rows_per_page_var]=$this->options[rows_per_page];
		  if(!isset($_GET[$nav_var])) $_GET[$nav_var]=0;
		  if(!isset($_GET[$numrows_var])) $_GET[$numrows_var]=$tot_rows;
		 //set defaults if not declared aleady  ends here


  $maxpagelinks=$this->options[pagelinksperpage];

  $tot_pages=ceil($_GET[$numrows_var]/$_GET[$rows_per_page_var])-1;
  $firstpage=$_GET[$nav_var]>$maxpagelinks?$_GET[$nav_var]-$maxpagelinks:0;
  $lastpage=($tot_pages>=($_GET[$nav_var]+$maxpagelinks))?$_GET[$nav_var]+$maxpagelinks:$tot_pages;
  for($i=($firstpage);$i<=$lastpage;$i++)
  {
   $page_nav_num.=" <a class=\"$class\"  href=\"$_SERVER[PHP_SELF]?".sp_skip_param(array($numrows_var,$nav_var,$rows_per_page_var))."&$rows_per_page_var=$_GET[$rows_per_page_var]&$numrows_var=$_GET[$numrows_var]&$nav_var=$i\">".($i+1)."</a>";
  
  }//for($i=0;$i<=$numrows;$i++)
  if($firstpage!=0) $page_nav_num="...$page_nav_num";
  if($lastpage!=$tot_pages) $page_nav_num="$page_nav_num...";
  return $page_nav_num;
 
 }//function pagenav($tot_rows,$numrows_var,$rows_per_page_var,$nav_var,$class="")
 function get_display_row_limit($rows_per_page_var,$nav_var)
 {
        $maxRows = (!($_GET[$rows_per_page_var]>0))?$this->options[rows_per_page]:$_GET[$rows_per_page_var];
        $pageNum = (!isset($_GET[$nav_var]))?0:$_GET[$nav_var];
        $startRow = ($pageNum * $maxRows)+1;
		$maxRows=$startRow + $maxRows-1;
		$maxRows=$maxRows<$this->row_count?$maxRows:$this->row_count;
		$limit="$startRow  to $maxRows of ";	
		return $limit;
 }//function get_row_limit($rows_per_page_var,$nav_var)
 function get_row_limit($rows_per_page_var,$nav_var)
 {
        $maxRows = (!($_GET[$rows_per_page_var]>0))?$this->options[rows_per_page]:$_GET[$rows_per_page_var];
        $pageNum = (!isset($_GET[$nav_var]))?0:$_GET[$nav_var];
        $startRow = $pageNum * $maxRows;
		$limit="limit $startRow ,$maxRows";	
		return $limit;
 }//function get_row_limit($rows_per_page_var,$nav_var)

 function fpnllinks($numrows,$rows_per_page)
 {
 }//function fpnllinks($numrows,$rows_per_page)
}//class pagenav
?>
Return current item: Page nav class