require_once 'Pager/Pager.php';
require_once 'MDB2.php';
//skipped the db connection code...
//let's just suppose we have a valid db connection in $db.
//first, we use Pager to create the links
$num_products = $db->queryOne('SELECT COUNT(*) FROM products');
$pager_options = array(
'mode' => 'Sliding',
'perPage' => 10,
'delta' => 2,
'totalItems' => $num_products,
);
$pager = Pager::factory($pager_options);
//then we fetch the relevant records for the current page
list($from, $to) = $pager->getOffsetByPageId();
//set the OFFSET and LIMIT clauses for the following query
$db->setLimit($pager_options['perPage'], $from - 1);
$query = 'SELECT prod_name, prod_description FROM products';
$products = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
//show the results
echo '
- ';
- '.$product['prod_name'].': '.$product['prod_description'].' ';
foreach ($products as $product) {
echo '
}
echo '
//show the links
echo $pager->links;
?>
Reference
No comments:
Post a Comment