#native_company# #native_desc#
#native_cta#

MySQLPagedResults

By Nate Sanden
on February 4, 2004

Version: 1.11

Type: Class

Category: Databases

License: GNU General Public License

Description: This is class creates 1,2,3… links and previous/next links which will page through any records from a mysql database table. It can easily be modified (one line) to use with any database however.

Put this class in its own file called MySQLPagedResults.class.php

<?php

class MySQLPagedResults {

   var $total_results_sql;
   var $current_page;
   var $results_per_page;
   var $links_per_page;
   var $previous_link_text;
   var $next_link_text;

   function MySQLPagedResults($total_results_sql,$page_name,$url_string,$results_per_page,$links_per_page,$first_link_text,$previous_link_text,$next_link_text,$last_link_text,$seperator) {
      $this->total_results_sql = $total_results_sql;
      if(!isset($_GET[$page_name])) {
         $this->current_page = 1;
      } else {
         $this->current_page = $_GET[$page_name];
      }
      $this->results_per_page = $results_per_page;
      $this->links_per_page = $links_per_page;
      $this->previous_link_text = $previous_link_text;
      $this->next_link_text = $next_link_text;
      $this->first_link_text = $first_link_text;
      $this->last_link_text = $last_link_text;
      $this->page_name = $page_name;
      $this->url_string = $url_string;
      $this->seperator = $seperator;
   }

   function totalResults() {
      $query = mysql_query($this->total_results_sql);
      $result = mysql_fetch_array($query);
      return $result[0];
   }

   function totalPages() {
      return ceil($this->totalResults()/$this->results_per_page);
   }

   function currentOffset() {
      return ($this->current_page-1)*$this->results_per_page;
   }

   function isFirstPage() {
      return ($this->current_page <= 1);
   }

   function isLastPage() {
      return ($this->current_page >= $this->totalPages());
   }

   function getPrevNav() {
      $nav='';
      //Deal with previous link
      if(!$this->isFirstPage()) {
         $nav.='<a href="?'.$this->page_name.'='.($this->current_page-1).''.$this->url_string.'">'.$this->previous_link_text.'</a>';
      } else {
         $nav.=$this->previous_link_text;
      }
      return $nav;
   }

   function getNextNav() {
      //Deal with next link
      if(!$this->isLastPage()) {
         $nav.='<a href="?'.$this->page_name.'='.($this->current_page+1).''.$this->url_string.'">'.$this->next_link_text.'</a>';
      } else {
         $nav.=$this->next_link_text;
      }
      return $nav;
   }

   function getFirstNav() {
      $nav='';
      //Deal with previous link
      if(!$this->isFirstPage()) {
         $nav.='<a href="?'.$this->page_name.'=1'.$this->url_string.'">'.$this->first_link_text.'</a>';
      } else {
         $nav.=$this->first_link_text;
      }
      return $nav;
   }

   function getLastNav() {
      $nav='';
      //Deal with previous link
      if(!$this->isLastPage()) {
         $nav.='<a href="?'.$this->page_name.'='.$this->totalPages().''.$this->url_string.'">'.$this->last_link_text.'</a>';
      } else {
         $nav.=$this->last_link_text;
      }
      return $nav;
   }

   function getResultNumbersStart() {
      return ($this->current_page*$this->results_per_page)-$this->results_per_page+1;
   }

   function getResultNumbersEnd() {
      return $this->getResultNumbersStart()+$this->results_per_page-1;
   }

   function getStartNumber() {
      $links_per_page_half = $this->links_per_page/2;
      if($this->current_page<=$links_per_page_half) {
         return 1;
      } elseif($this->current_page>=$this->totalPages()-$links_per_page_half) {
         return $this->totalPages()-$this->links_per_page+1;
      } else {
         return $this->current_page-$links_per_page_half;
      }
   }

   function getEndNumber() {
      if($this->totalPages() < $this->links_per_page) {
         return $this->totalPages();
      } else {
         return $this->links_per_page;
      }
   }

   function getPagesNav() {
      $nav='';
      for($i=$this->getStartNumber(); $i<$this->getStartNumber()+$this->getEndNumber(); $i++) {
         if($i!=$this->current_page) {
            $nav.="<a href='?".$this->page_name."=$i".$this->url_string."'>$i</a>";
         } else {
            $nav.="$i";
         }
         if($i!=$this->getStartNumber()+$this->getEndNumber()-1) { $nav.=$this->seperator; }
      }
      return $nav;
   }

}

?>

Then use the following code to make use of the class.

<?php

//Paging class
include("MySQLPagedResults.class.php");

//Database connection
include("db.inc.php");

//Instantiate a new instance of the class
$paging_results = new MySQLPagedResults("SELECT count(*) FROM mytable","page","",25,10,"<<","Previous","Next",">>"," | ");

//Make the variables from the class easier to read/use
$first_nav = $paging_results->getFirstNav();
$prev_nav = $paging_results->getPrevNav();
$next_nav = $paging_results->getNextNav();
$last_nav = $paging_results->getLastNav();
$pages_nav = $paging_results->getPagesNav();
$offset = $paging_results->currentOffset();
$results_per_page = $paging_results->results_per_page;
$current_page = $paging_results->current_page;
$total_pages = $paging_results->totalPages();
$start_number = $paging_results->getResultNumbersStart();
$end_number = $paging_results->getResultNumbersEnd();

//Loop through a table of our database using the $offset and $results_per_page variables from our class.
$sql = "SELECT * FROM mytable LIMIT $offset,$results_per_page";
$query = mysql_query($sql);
while($data = mysql_fetch_array($query)) {
   echo $data[0] . ", ";
}

echo "<p><b>Page:</b> $current_page of $total_pages &nbsp;|&nbsp; <b>Results:</b> $start_number - $end_number<br>";
echo "$first_nav $prev_nav $pages_nav $next_nav $last_nav</p>";

?>