Location: PHPKode > scripts > tGrid > tgrid/paging.php
<?php
/* ***************************************************
Class PagedResults v 1.0
(c) budi laxono (28-05-2008)
class to create data paging

**************************************************** */

class PagedResults {

   /* These are defaults */
   var $TotalResults;
   var $CurrentPage = 1;
   var $PageVarName = "page";
   public $ResultsPerPage = 10;
   var $LinksPerPage = 6;
   var $param=array();


   function InfoArray() {
      $this->TotalPages = $this->getTotalPages();
      $this->CurrentPage = $this->getCurrentPage();
      $this->ResultArray = array(
                           "PREV_PAGE" => $this->getPrevPage(),
                           "NEXT_PAGE" => $this->getNextPage(),
                           "CURRENT_PAGE" => $this->CurrentPage,
                           "TOTAL_PAGES" => $this->TotalPages,
                           "TOTAL_RESULTS" => $this->TotalResults,
                           "PAGE_NUMBERS" => $this->getNumbers(),
                           "SQL_LIMIT1" => $this->getStartOffset()==0?$this->getStartOffset():$this->getStartOffset()-1,
                           "SQL_LIMIT2" => $this->ResultsPerPage,
                           "START_OFFSET" => $this->getStartOffset(),
                           "END_OFFSET" => $this->getEndOffset(),
                           "RESULTS_PER_PAGE" => $this->ResultsPerPage,
                           );
      return $this->ResultArray;
   }

   /* Start information functions */
   function getTotalPages() {
      /* Make sure we don't devide by zero */
      if($this->TotalResults != 0 && $this->ResultsPerPage != 0) {
         $result = ceil($this->TotalResults / $this->ResultsPerPage);
      }
      /* If 0, make it 1 page */
      if($result == 0) {
         return 1;
      } else {
         return $result;
      }
   }

   function getStartOffset() {
      $offset = $this->ResultsPerPage * ($this->CurrentPage - 1);
      if($offset != 0) { $offset++; }     
      return $offset;
   }

   function getEndOffset() {
      if($this->getStartOffset() > ($this->TotalResults - $this->ResultsPerPage)) {
         $offset = $this->TotalResults;
      } elseif($this->getStartOffset() != 0) {
         $offset = $this->getStartOffset() + $this->ResultsPerPage - 1;
      } else {
         $offset = $this->ResultsPerPage;
      }
      return $offset;
   }

   function getCurrentPage() {
      $page = $_REQUEST[$this->PageVarName];
      if(!$page) { return $this->CurrentPage; } else { return $page; }
   }

   function setCurrentPage($page) {
        $this->ResultArray['CURRENT_PAGE'] = $page;
   }

   function getPrevPage() {
      if($this->CurrentPage > 1) {
         return $this->CurrentPage - 1;
      } else {
         return false;
      }
   }

   function getNextPage() {
      if($this->CurrentPage < $this->TotalPages) {
         return $this->CurrentPage + 1;
      } else {
         return false;
      }
   }

   function getStartNumber() {
      $links_per_page_half = $this->LinksPerPage / 2;
      /* See if curpage is less than half links per page */
      if($this->CurrentPage <= $links_per_page_half || $this->TotalPages <= $this->LinksPerPage) {
         return 1;
      /* See if curpage is greater than TotalPages minus Half links per page */
      } elseif($this->CurrentPage >= ($this->TotalPages - $links_per_page_half)) {
         return $this->TotalPages - $this->LinksPerPage + 1;
      } else {
         return $this->CurrentPage - $links_per_page_half;
      }
   }

   function getEndNumber() {
      if($this->TotalPages < $this->LinksPerPage) {
         return $this->TotalPages;
      } else {
         return $this->getStartNumber() + $this->LinksPerPage - 1;
      }
   }

   function getNumbers() {
      for($i=$this->getStartNumber(); $i<=$this->getEndNumber(); $i++) {
         $numbers[] = $i;
      }
      return $numbers;
   }
   
   function serialize(){
   	    $ser='';
		$num = count($_POST);
		$i=1;
		foreach($_POST as $key=>$value){
			$ser .= $key."=".$value;	
			if($i==$num) break;
			$i++;
			$ser .= "&";	
		}
		return $ser;   
   }

   function createLinks($url,$param,$container = "\$('main-cont')",$numberLinkName='pagingLink') { 
   
        if(stripos($url, "?")) {$mark = "&";} else{$mark = "?";}
		/* Print our some info like "Displaying page 1 of 49" */
       echo "Displaying page " . $this->ResultArray["CURRENT_PAGE"] . " of " . $this->ResultArray["TOTAL_PAGES"] . " ";
       /* Print our first link */
       if($this->ResultArray["CURRENT_PAGE"]!= 1) {
          echo "<a href='$url' id='" .$numberLinkName . "First' url='".$mark."page=1'>&lt;&lt;</a> ";
       } else {
          echo "&lt;&lt; ";
       }

       /* Print out our prev link */
       if($this->ResultArray["PREV_PAGE"]) {
          echo "<a href='$url".$mark."page=" . $this->ResultArray["PREV_PAGE"] . "' id='" .$numberLinkName . "Prev'>Previous</a> | ";
       } else {
          echo "Previous | ";
       }

	    /* Example of how to print our number links! */
       for($i=0; $i<count($this->ResultArray["PAGE_NUMBERS"]); $i++) {
          if($this->ResultArray["CURRENT_PAGE"] == $this->ResultArray["PAGE_NUMBERS"][$i]) {
             echo "[".$this->ResultArray["PAGE_NUMBERS"][$i] . "] | ";
          } else {
             echo "<a href='$url".$mark."page=" . $this->ResultArray["PAGE_NUMBERS"][$i] . "' id='" .$numberLinkName . $i."'>" . $this->ResultArray["PAGE_NUMBERS"][$i] . "</a> | ";
          }
       }

       /* Print out our next link */
       if($this->ResultArray["NEXT_PAGE"]) {
          echo " <a href='$url".$mark."page=" . $this->ResultArray["NEXT_PAGE"] . "' id='" .$numberLinkName . "Next'>Next</a>";
       } else {
          echo " Next";
       }

       /* Print our last link */
       if($this->ResultArray["CURRENT_PAGE"]!= $this->ResultArray["TOTAL_PAGES"]) {
          echo " <a href='$url".$mark."page=" . $this->ResultArray["TOTAL_PAGES"] . "' id='" .$numberLinkName . "Last'>>></a>";
       } else {
          echo " &gt;&gt;";
       }
   }

}

?> 
Return current item: tGrid