Sign in to follow this  
  • entries
    43
  • comments
    51
  • views
    31883

Paging Owns Me

Sign in to follow this  

238 views

So it looks like I've completed all the login/registration stuff and I've implemented member profiles and member profile updating (by the owners and admins). While writing the profile system I've decided that in some later version of the CMS modules will be required to supply member information (if any) so that all the member information can be viewed in a central location in order to more unify the site.

The last part I need to create in the admin module is the ability to view the members. In this version I wont have sorting capabilities but that's for one main reason: paging owns me. I would like to create a PHP class that allows me to implement paging elsewhere but it seems that paging is very dependent on the actual data that is being paged. On top of that, the paging class will need to be rewritten if databases other than MySQL (or perhaps Postgres) are used since it looks like I need to use LIMIT. The most difficult thing I have found is that two different queries are apparently needed to get the number of pages in the data and the actual data for the page.

This is my current class


/************************************************************************
*
* Title: Paging Manager
* Author: Colin Jeanne (http://colinjeanne.net)
* Date: June 04, 2005
*
* Description:
* Defines the PagingManager class
* Simplifies the paging of data
*
************************************************************************/


require_once('db-class.php');

// Simplifies the paging of data by modifying an SQL query to only return
// the rows on the current page
class PagingManager {

var $query;
var $render_function;
var $number_function;
var $items_per_page;
var $max_pages;

// Sets up the PagingManager object
function PagingManager($query, $render_function, $number_function,
$items_per_page, $max_pages) {
$this->query = $query;
$this->render_function = $render_function;
$this->number_function = $number_function;
$this->items_per_page = (int)$items_per_page;
$this->max_pages = (int)$max_pages;
$this->result = false;
}

// Retrieves rows from the data base and sends them as an associated
// array to the rendering function
// $pagenum is a 1-based index
function ShowPage($pagenum) {
$db = new Database;
$db->Connect();

if ($db->error != '')
return false;

$result = $db->Query($this->query . ' LIMIT ' .
$this->items_per_page * ($pagenum - 1) . ', ' .
$this->items_per_page);

if ($result == false) {
$db->Disconnect();
return false;
}

while (($data = $result->FetchAssoc()) != false)
call_user_func($this->render_function, $data);

$result->FreeResult();
$db->Disconnect();

return true;
}

// Returns the number of pages in the query
function GetNumPages($query) {
$db = new Database;
$db->Connect();

if ($db->error != '')
return false;

$result = $db->Query($query);

if ($result == false) {
$db->Disconnect();
return false;
}

$data = $result->FetchRow();

$result->FreeResult();
$db->Disconnect();

return ceil($data / $this->items_per_page);
}

// Prints the list of page numbers
function ShowNumberList($query) {
$pages = $this->GetNumPages($query);

if ($pages == false)
return false;

for ($i = 1; $i <= $this->max_pages; $i++)
call_user_func($this->number_function, $i);

return true;
}
}
?>



I havent tested it yet so there might be some bugs or syntax errors. I made render_function and number_function callbacks since the point of this class was that it only helps with paging data not how it will be displayed.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now