I'll take those comments as a yes

so I hacked something up quick to show some running code for what I proposed above:
Code: Select all
class PagerArrayDatasource {
var $data = array();
function PagerArrayDatasource($data) {
if (is_array($data)) {
$this->data = $data;
}
}
function getNumRows() {
return count($this->data);
}
}
class Pager {
var $datasource = null;
var $page_size = 10;
var $current_page = 0;
var $max_page = 0;
var $max_row = 0;
var $start_row = 0;
var $end_row = 0;
var $page_parameter = 'page';
function Pager(&$datasource) {
$this->datasource = &$datasource;
}
function setPageSize($n) {
if ($n > 0) {
$this->page_size = $n;
}
}
function setCurrentPage($n) {
$this->max_row = $this->datasource->getNumRows();
if ($this->max_row > 0) {
$this->max_page = ceil($this->max_row / $this->page_size);
if ($n < 1) {
$n = 1;
}
if ($n > $this->max_page) {
$n = $this->max_page;
}
$this->current_page = $n;
$this->start_row = ($n - 1) * $this->page_size + 1;
$this->end_row = $this->start_row + $this->page_size - 1;
if ($this->end_row > $this->max_row) {
$this->end_row = $this->max_row;
}
} else {
$this->max_page = 0;
$this->current_page = 0;
$this->start_row = 0;
$this->end_row = 0;
}
}
}
class PagerHTMLOutputter {
var $pager = null;
function PagerHTMLOutputter(&$pager) {
$this->pager = &$pager;
}
function debug() {
return "
page_size={$this->pager->page_size},
current_page={$this->pager->current_page},
max_page={$this->pager->max_page},
max_row={$this->pager->max_row},
start_row={$this->pager->start_row},
end_row={$this->pager->end_row},
";
}
}
// initialize an array for testing
for ($i=0; $i<=75; ++$i) {
$myarray[$i] = 'This is row ' . $i;
}
// create a data object that has the interface needed by the Pager object
$datasource = & new PagerArrayDatasource($myarray);
// create pager using values from datasource and request parameters
$pager = & new Pager($datasource);
// create Outputter that gets its values from Pager object
$output = & new PagerHTMLOutputter($pager);
// initialize an array to testing
for ($n=-1; $n<=10; ++$n) {
$pager->setCurrentPage($n);
echo "<p>page=$n: " . $output->debug() . '</p>';
}
This code should work in PHP4 and PHP5 (only checked in PHP 5 though). I created debug data with 75 rows which should be 8 pages with the default of 10 rows per page. I then check it with page values from -1 .. 10 (to check bounds as well). The output looks like this:
page=-1: page_size=10, current_page=1, max_page=8, max_row=76, start_row=1, end_row=10,
page=0: page_size=10, current_page=1, max_page=8, max_row=76, start_row=1, end_row=10,
page=1: page_size=10, current_page=1, max_page=8, max_row=76, start_row=1, end_row=10,
page=2: page_size=10, current_page=2, max_page=8, max_row=76, start_row=11, end_row=20,
page=3: page_size=10, current_page=3, max_page=8, max_row=76, start_row=21, end_row=30,
page=4: page_size=10, current_page=4, max_page=8, max_row=76, start_row=31, end_row=40,
page=5: page_size=10, current_page=5, max_page=8, max_row=76, start_row=41, end_row=50,
page=6: page_size=10, current_page=6, max_page=8, max_row=76, start_row=51, end_row=60,
page=7: page_size=10, current_page=7, max_page=8, max_row=76, start_row=61, end_row=70,
page=8: page_size=10, current_page=8, max_page=8, max_row=76, start_row=71, end_row=76,
page=9: page_size=10, current_page=8, max_page=8, max_row=76, start_row=71, end_row=76,
page=10: page_size=10, current_page=8, max_page=8, max_row=76, start_row=71, end_row=76,