Posted: Mon Feb 20, 2006 11:39 pm
I have not worked on this for a while, but got the ADODB and MySQL Pageable classes working so I thought I would bundle up the code. It is available here:
http://ap3.sourceforge.net/pager-0.2.0.zip
One other thing I wanted to add, because I use it, is to allow the pager to remember where it was and resume. This is very helpful in CRUD type code because the paged list contains a link for each item in the list. When you are done on the sub page for that list item, it is much nicer to return to the list where you left off. Sessions are needed to do that without requiring the sub pages to remember values and pass them back. Unfortunately the logic is messy and the code I wrote just seems terrible to me (and has a terrible name). Maybe someone can find a way to clean up this code:
http://ap3.sourceforge.net/pager-0.2.0.zip
One other thing I wanted to add, because I use it, is to allow the pager to remember where it was and resume. This is very helpful in CRUD type code because the paged list contains a link for each item in the list. When you are done on the sub page for that list item, it is much nicer to return to the list where you left off. Sessions are needed to do that without requiring the sub pages to remember values and pass them back. Unfortunately the logic is messy and the code I wrote just seems terrible to me (and has a terrible name). Maybe someone can find a way to clean up this code:
Code: Select all
class PagerSessionRequest {
var $pager;
var $session_name = 'Pager';
var $page_resume = 'resume';
var $last_row_recalc = 'recalc';
function PagerSessionRequest(&$pager) {
$this->pager = &$pager;
}
function process() {
session_start();
$pager = &$this->pager;
$resume = false;
// code to set the page number
// no 'page=' parameter so initialize pager
if (! isset($_GET[$pager->page_param])) {
$pager->current_page = $pager->first_page;
unset($_SESSION[$this->session_name]); // clear any previous values
$_SESSION[$this->session_name]['page'] = $pager->current_page;
} else {
// 'page=resume' and page number in session so resume
if (($_GET[$pager->page_param] === $this->page_resume) && (isset($_SESSION[$this->session_name]['page']))) {
$pager->current_page = $_SESSION[$this->session_name]['page'];
$resume = true;
} else {
// use 'page=' parameter instead
$pager->current_page = intval($_GET[$pager->page_param]);
$_SESSION[$this->session_name]['page'] = $pager->current_page;
}
}
// code to set the last row value
// no 'last_row=' param and not resuming OR 'last_row=recalc' to force the datasource to recalculate value
if ((! isset($_GET[$pager->last_row_param]) && (! $resume))
|| ((isset($_GET[$pager->last_row_param]) && ($_GET[$pager->last_row_param] === $this->last_row_recalc)))) {
$pager->last_row = $pager->datasource->getNumRows();
$_SESSION[$this->session_name]['last_row'] = $pager->last_row;
} elseif (isset($_SESSION[$this->session_name]['last_row'])) {
// last_row value is in session so use that
$pager->last_row = $_SESSION[$this->session_name]['last_row'];
} else {
// set the value in the session to the value in the pager object
$_SESSION[$this->session_name]['last_row'] = $pager->last_row;
}
// code to set the page size value
// 'page_size=' parameter passed so save in session
if (isset($_GET[$pager->page_size_param])) {
$pager->page_size = intval($_GET[$pager->page_size_param]);
$_SESSION[$this->session_name]['page_size'] = $pager->page_size;
} elseif (isset($_SESSION[$this->session_name]['page_size'])) {
// page size value is in session so use that value
$pager->page_size = $_SESSION[$this->session_name]['page_size'];
} else {
// set the value in the session to the value in the pager object
$_SESSION[$this->session_name]['page_size'] = $pager->page_size;
}
$pager->setCurrentPage($pager->current_page);
}
}