Location: PHPKode > scripts > SQL Data Browsing > sql-data-browsing/pagination.class.php
<?php

/*
  ****************************************************************************
  * class pagination                                                         *
  * Version 1.2                                                              *
  *                                                                          *
  * A PHP class for browsing through a large set of data                     *
  *                                                                          *
  * Copyright (C) 2003 by Dragos Protung - hide@address.com                 *
  *                                                                          *
  * This PHP class is free software; you can redistribute it and/or          *
  * modify it under the terms of the GNU Lesser General Public               *
  * License as published by the Free Software Foundation; either             *
  * version 2.1 of the License, or (at your option) any later version.       *
  *                                                                          *
  * This PHP class is distributed in the hope that it will be useful,        *
  * but WITHOUT ANY WARRANTY; without even the implied warranty of           *
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU         *
  * Lesser General Public License for more details.                          *
  *                                                                          *
  *                                                                          *
  * Author:                                                                  *
  * Dragos Protung, 500164 Brasov, Romania, hide@address.com                *
  *                                                                          *
  ****************************************************************************
*/

class pagination {

   /*
    # @function  pagination()       --  constructor
    #
    # @param     $sql_result        --  the result of the query
    # @param     $cur_page          --  the curent page
    # @param     $records_per_page  --  how many record are shown per page
    # @param     $link              --  the link to get to another page. After the link string the page number is attached
    # @param     $link_text_patern  --  the patern for the links to the other pages
    #
   */

   function pagination($sql_result, $cur_page, $records_per_page, $link, $link_text_patern = "[%s]"){


      $this -> sql_result        = $sql_result;
      $this -> sql_num_rows      = @mysql_num_rows($this -> sql_result);
      $this -> sql_num_cols      = @mysql_num_fields($this -> sql_result);
      $this -> link              = $link;
      $this -> link_text_patern  = $link_text_patern;
      $this -> cur_page          = $cur_page;
      $this -> records_per_page  = $records_per_page;
      $this -> total_pages       = ceil($this -> sql_num_rows / $this -> records_per_page);
      $this -> val_min_de_afisat = 0;
      $this -> val_min_de_afisat = 0;
   }

   /*
    #
    # @function   show_pagination() --  echos the links for pagination
    #
   */

   function show_pagination() {

      if ($this -> total_pages > $this -> cur_page) {
	        $this -> val_min_de_afisat = $this -> cur_page - ($this -> cur_page % 10);
	        $this -> val_max_de_afisat = $this -> val_min_de_afisat + 10;

	        if ($this -> cur_page > 9) echo ("<a href=\"" . $this -> link . ($this -> val_min_de_afisat-1) . "\"> << </a>\n");
	        for ($i = $this -> val_min_de_afisat; $i < min($this -> val_max_de_afisat, $this -> total_pages); $i++) {

	           if ($this -> cur_page < $i)  printf ("<a href=\"" . $this -> link . ($i) . "\">" . $this -> link_text_patern . "</a>\n", $i+1);
	           if ($this -> cur_page == $i) printf ($this -> link_text_patern . "\n", $i+1);
	           if ($this -> cur_page > $i)  printf ("<a href=\"" . $this -> link . ($i) . "\">" . $this -> link_text_patern . "</a>\n", $i+1);
	        }
	        if ($this -> val_max_de_afisat < $this -> total_pages) printf ("<a href=\"" . $this -> link . ($i) . "\"> >> </a>\n", $i+1);
      }
   }
}
?>
Return current item: SQL Data Browsing