#native_company# #native_desc#
#native_cta#

clsPaging() – paging class for mysql recordsets

By Tim Johannessen
on December 30, 2003

Version: 1.0

Type: Class

Category: Databases

License: GNU General Public License

Description: A simple class to page a MySQL recordset.

<?
/*****************************************************************************************

Project:	clsPaging();
Version:	1.0
Date:		december 30th, 2003	
Author:		Tim Johannessen aka zubfatal
E-mail:		[email protected]

Description:
Simple class to page a MySQL recordset.

Usage:
$cnmysql = mysql_connect("mysql_server", "mysql_user", "mysql_pass");
mysql_select_db("mysql_database", $cnmysql);

$objPage = new clsPaging(); // initialize the class.
$objPage->objMySQL = $cnmysql; // set the mysql resource.
$objPage->pagingLimit = 25; // set the paging limit - default is 25.
$objPage->strNext = "Next [num]"; // [num] is replaced with the paginglimit.
$objPage->strPrev = "Prev [num]"; // [num] is replaced with the paginglimit.
$objPage->strLast = "Last [num]"; // [num] is replaced with the last number of records.
$objPage->query("select * from sometable"); // returns an array with the records 


Return values:
function: query(string sqlquery) - returns an array with the records.
function: getmicrotime() - used to determine how long it takes to page the recordset.
int: offset - returns the current offset
int: numRows - returns the number of records, before paging is done.
float: parsetime - returns the time spend paging the recordset.
int: currentPage - returns the current page number.
int: numPages - returns the number of pages.
int: recordsLeft - returns the number of records left from the current offset.

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


class clsPaging {
	var $objMySQL = "";
	var $pagingLimit = 25;
	var $strNext = "Next [num] &raquo;";
	var $strPrev = "&laquo; Prev [num]";
	var $strLast = "Last [num] &raquo;";
	
	function query($strSQL = "") {
		if (!$this->objMySQL) { die("ERROR: not a valid MySQL resource?"); }
		if (empty($strSQL)) { die("ERROR: no SQL query?"); }
		
		$time_start = $this->getmicrotime();
		
		$this->numResults = mysql_num_rows(mysql_query($strSQL)) or die("MySQL Error: ". mysql_errno() ."n". mysql_error());
		$this->numPages = ceil($this->numResults / $this->pagingLimit);
		
		if ($numRows % $this->pagingLimit) { $this->numPages++; }
		if (($this->currentPage > $this->numPages) || (!is_numeric($this->currentPage))) { $this->currentPage = 1; }
		
		$this->recordsLeft = intval($this->numResults - ($this->currentPage * $this->pagingLimit));
		$this->offset = intval($this->currentPage * $this->pagingLimit);
		$this->offset = intval($this->offset - $this->pagingLimit);
		
		$this->strPrev =  str_replace("[num]", $this->pagingLimit, $this->strPrev);
		$this->strNext =  str_replace("[num]", $this->pagingLimit, $this->strNext);
		$this->strLast =  str_replace("[num]", $this->recordsLeft, $this->strLast);
		
		$strSQL .= " limit ". $this->offset .", ". $this->pagingLimit;
		
		$objResults = mysql_query($strSQL);
		$arrResults = array();
		
		while ($resultRow = mysql_fetch_array($objResults, MYSQL_ASSOC)) {
			$arrResults[] = $resultRow;
		}
		
		$this->parsetime = $this->getmicrotime() - $time_start;
		
		return $arrResults;
		mysql_free_result($objResults);
	}
	
	function getmicrotime() {
	    list($usec, $sec) = explode(" ",microtime());
	    return ((float)$usec + (float)$sec);
	}
	
}

?>