Load using pager from table

db.php has nice pager. When using \db\pager table handler automaticaly calculates total count of results and paginates to specified size of chunks.

$pager = new \db\pager(integer $page, integer $count);

page is current page

count is count of items per page

To use pager simply pass it to load function:

$database->path->to->class->load (\db\pager $pager);


$pager = new \db\pager (null, 8);

$products = $database->shop->product->load ($pager);

echo count ($products);

Outputs 8 if items in products table are not less than 8

To load items from page 2:

$pager = new \db\pager (3, 8);
$products = $database->shop->product->load ($pager);


Pager object will look like this if we have total 30 products in table:

      'page'  =  3,
      'pages'  =  4,
      'total'  =  30,
      'count'  =  8,
      'from'  =  16,

Current page


Number of pages available


Total items in table


Item count per page


First item number of result


Table handler simply uses

... limit $pager->from, $pager->count

To generate query

To be simple you can do pager loading in one line:

$database->shop->product->load(new \db\pager($_REQUEST['page'], 10));

Where $_REQUEST['page'] holds current page number. If you pass greater number in page parameter than available pages than current page will be set to last page. If you pass page number less than 1 or null or not integer than current page will be set to 1.

Pager also has method next it is rarely usefull but allows to load items from table by small chunks not to overfill memory and iterate through them:

$pager = new \db\pager (null, 10);

    $result = $database->shop->product->load ($pager);
    if ($result)
        foreach ($result as $product)

while ($pager->next());

This will iterate throgh every product and will load them 10 by 10. Sometimes if you have 99999 records in table and you have to affect them all it is extremly important not to load them all because objects in result might overload memory if they are large.