<?php
/*
-------------------------------------------------------------------------
dbResult Class
An abstract class to be used as a parent class for sqlite, mysql, etc... to handle DB result sets.
-------------------------------------------------------------------------
Developer
Name -- Haddad Said.
Date -- 10-08-2005
Version -- 1.2
-------------------------------------------------------------------------
Member Functions
3.SetPaging -- Initializes the paging result set attributes
4.getPages -- Returns number of pages for the set
5.setPage -- Sets the current page of the set
6.getPage -- Returns the current page of the set
7.isLastPage -- Returns true if the current page is the last page
8.isFirstPage -- Returns true if the current page is the first page
9.getPageNav -- Returns the paged set navigation system
-------------------------------------------------------------------------
*/
require_once("class.db.cnx.php");
class dbResult
{
//Variable to hold the connectivity object
var $db_cnx;
//Variable to hold the mysql result resource
var $db_result;
//Some variables for the paged results functions
var $pg_size;
var $pg_page;
var $pg_row;
/*
-------------------------------------------------------------------------
Class Constructor
Parameters:
$db - The db connectivity object's reference
$result - database resultset resource.
Notes:
connectivity object is passed by reference and the result resource is pased by value
Example Usage:
-------------------------------------------------------------------------
*/
function dbResult(&$db_cnx, $result)
{
$this->db_cnx = &$db_cnx;
$this->db_result = $result;
}
/*
-------------------------------------------------------------------------
Method setPaging
Initializes the paged result set attirbutes
Parameters:
size -- Number of results to display in a single page.
Notes:
This method should be called after $this->do_query to avoid errors.
It traces for tha $page variable in $_GET to handle which page to display
Example Usage:
$db->setPaging(5) -- Sets 5 results per page
-------------------------------------------------------------------------
*/
function setPaging($size, $page)
{
//Intialize some object variables.
$this->pg_size = $size;
//Here we ensure that $page does not fall out of the bottom or top boundary of pages
if((int)$page <= 0)
{
$page = 1;
}
if($page > $this->getPages())
{
$page = $this->getPages();
}
//Then we proceed to set the active page
$this->setPage($page);
}
/*
-------------------------------------------------------------------------
Method getPages
Returns the number of pages in the set
Parameters:
Notes:
This method uses $this->db_result to calculate number of pages
So call $this->do_query first before using it.
Mostly used for internal functions anyway. (probably should be a private method)
Example Usage:
n.a
-------------------------------------------------------------------------
*/
function getPages()
{
if(!$this->db_result)
{
return false;
}
//Divide number of rows in result by number of result for each page, round
// to the nearest top value and get number of pages -- simple maths, eh?
return ceil($this->getNumRows()/(float)$this->pg_size);
}
/*
-------------------------------------------------------------------------
Method setPage
Sets the current page of the set
Parameters:
page -- integer to be used as a page number
Notes:
Mostly internal usage, should be private
Example Usage:
Look in function setPaging() above.
-------------------------------------------------------------------------
*/
function setPage($page)
{
if(($page > $this->getPages()) || ($page <= 0))
{
return false;
}
$this->pg_page = $page;
//Initialize the row counter to 0.
$this->pg_row = 0;
//Set the row seeker to the first row for the current page
$this->seek(($page - 1) * $this->pg_size);
}
/*
-------------------------------------------------------------------------
Method getPage
Returns the current page of the paged set
Parameters:
Notes:
A method to access the variable $this->pg_page.
Example Usage:
<p>Page <?=$db->getPage()?> of <?=$db->getPages()?></p>
-------------------------------------------------------------------------
*/
function getPage()
{
return $this->pg_page;
}
/*
-------------------------------------------------------------------------
Method isLastPage
Checks whether the current page is the last page or not
Parameters:
Notes:
Internal method
Example Usage:
Look at function getPageNav() below
-------------------------------------------------------------------------
*/
function isLastPage()
{
return ($this->pg_page >= $this->getPages());
}
/*
-------------------------------------------------------------------------
Method isFirstPage
Checks whether the current page is the first page or not
Parameters:
Notes:
Internal method
Example Usage:
Look at function getPageNav() below
-------------------------------------------------------------------------
*/
function isFirstPage()
{
return ($this->pg_page <= 1);
}
/*
-------------------------------------------------------------------------
Method getPageNav
Sets the page navigation for the paged set
Parameters:
arg_list -- a list of $_GET parameters to be appended on the links
Notes:
This method will build the navigation links to navigate your paged set
Most likely to be diplayed below your displayed records
It will only build the navigation links if there is more then one page in the set
Example Usage:
echo $db->getPageNav();
-------------------------------------------------------------------------
*/
function getPageNav($arg_list = '')
{
$pageNav = array('prev' => '', 'nav' => '', 'next' => '');
//If this is not the first page, we'll show a link to go to the previous page
if (!$this->isFirstPage())
{
$pageNav['prev'] = "$PHP_SELF?$arg_list&page=" . ($this->getPage() - 1) . "\"";
}
//If there is more than one page in the set, we build the navigation links here
if($this->getPages() > 1)
{
for ($i = 1; $i <= $this->getPages(); $i++)
{
//The current page doesnt have to be a clickable link
if($i == $this->pg_page)
{
$pageNav['nav'] .= "$i ";
}
//But others should be
else
{
$pageNav['nav'] .= "<a href=\"$PHP_SELF?$arg_list&page=$i\">$i</a> ";
}
}
//If this is not the last page, we'll show a link to go to the next page
if(!$this->isLastPage())
{
$pageNav['next'] = "$PHP_SELF?$arg_list&page=" . ($this->getPage() + 1) . "\"";
}
//Return the navigation links for formatting and display by the calling script
return $pageNav;
}
}
}
?>