To divide the list of records on pages in any model you need to create an object of “Pager” class. For this call "runPager($total, $limit)" methos, where "$total" - is a total number of records in a table (maybe with special conditions of filters), and "$limit" – number of records per a page.

We can get "$total" parameter by counting records with "countTotal()" method, and also we need to know at this moment which page of the list we are located at. For this we have "defineCurrentPage($start_key)" method in "$mv -> router" object.

Example of template content "view-events.php" file

//Count of active records (events) in a table
//If an array with parameters was not passed, then all records will be counted
$total_active = $mv -> events -> countRecords(array("active" => 1));

//Define a current page for URL like "events/page/2/"
$current_page = $mv -> router -> defineCurrentPage(1);
//Run a pagination by 5 records per page
$mv -> events -> runPager($total_active, 5, $current_page);

When defining a current page, the parameter "$start_key" equal to "1" indicates the possible position of page number in URL. In this case we deal with links of "events/page/2/","events/page/3/" formats and indicate to router where in URL to find the beginning of construction of "page/number-of-page/" format. In our case definition of page starts after "events" part, that is from an array element with an index of 1 (index 0 is for "events"). For example, for the links of “catalog/books/page/2/" format the parameter "$start_key" will equal to 2.

Also in "defineCurrentPage()" method it is possible to pass the name of GET parameter where a number of current page can be seated.

//For URL like "/news/?page=3"
$current_page = $mv -> router -> defineCurrentPage("page");

This way we can now add pagination into event display function in Events model. More details about record extraction are in Query Constructor section.

public function displayEvents()
    $rows = $this -> select(array("order->desc" => "date", 
                                  "limit->" => $this -> pager -> getParamsForSelect()));
    $html = "";

    foreach($rows as $row)

To display a list of links to other pages a "display($path, $smart [ ,$extra_params])" method of "Pager" class is used.
Description of parameters:

  • $path - an URL to which a number of ("news", "catalog/books/") pages will be added, a path from project root will be added to it in the beginning
  • $smart - when the value is "1", parameters of "page/3/" (true) format will be added to "$path", if the value is "0" (false) - parameters of "page=3" format will be added
  • $extra_params - (optional) additional parameters to URL ("active/", "sort=price&order=desc")
<div class="pager">
    <? echo $mv -> events -> pager -> display($mv -> root_path."events/", 1); ?>    

Final content of "view-events.php" file

$total_active = $mv -> events -> countRecords(array("active" => 1));
$mv -> events -> runPager($total_active, 5, $mv -> router -> defineCurrentPage(1));
include $mv -> views_path."main-header.php";

<div id="content">
    <? echo $mv -> events -> display(); ?>
    <div class="pager">
        <? echo $mv -> events -> pager -> display($mv -> root_path."events/", 1); ?>    

include $mv -> views_path."main-footer.php";

Methods of Pager Object

  • getTotal(), getLimit(), getStart() - return total number of elements, limit per a page, number of the first element in current interval accordingly
  • setLimit($limit), setTotal($total) - set a limit and total number of elements, count intervals
  • getIntervals() - number of intervals, received as a total number/limit per a page
  • getPage() - number of current page (interval)
  • display($path, $smart [, $extra_params]) - display a list of pages (described above)
  • displayLimits($limits, $path [, $options]) - display a list of links with GET parameters as "pager-limit=10", $limits - one-dimensional array of integers, $path – starting URL, to which GET parameters are added. If you pass an optional parameter $options as "options", then options for Select tag will be displayed instead of links.
  • hasPages() - checks if there are intervals (number of intervals > 1)
  • checkPrevNext($type) - checks if there is next/previous page from the current one, $type parameter should have "next" or "prev" values.
  • displayPrevLink($caption, $path) - displays a link to previous page if exists, $caption – is text of the link, $path - URL, to which a parameter of "page=3" format will be added
  • displayNextLink($caption, $path) - similar to previous function - displays a link to next page
  • addUrlParams($path) - adds GET parameter of "page=7" format (current page) to the passed URL, inserts "?" or "&" itself if needed. Usually is being used to transfer parameters of pagination into other modules (sorting, filter).
  • getUrlParams() - returns GET parameter of "page=12" format (current page), if total number of pages > 1
$limits = array(8, 16, 32, 64);
if(isset($_GET["pager-limit"]) && in_array($_GET["pager-limit"], $limits))
    $limit = $_SESSION["pager-limit"] = intval($_GET["pager-limit"]);
else if(isset($_SESSION["pager-limit"]) && in_array($_SESSION["pager-limit"], $limits))
    $limit = intval($_SESSION["pager-limit"]);
    $limit = 16;
$count_products = $mv -> products -> countRecords(array("active" => 1, "parent" => $catalog -> id));
$current_page = $mv -> router -> defineCurrentPage("page");
$mv -> products -> runPager($count_products, $limit, $current_page);
$pager_url = $mv -> root_path."catalog/45/"; 
<div id="pager-and-limiter">
    <div class="limiter">
        Objects on a page :
        <? echo $mv -> products -> pager -> displayLimits($limits, $pager_url); ?>
    <div class="pager">
            echo $mv -> products -> pager -> displayPrevLink("previous", $pager_url);
            echo $mv -> products -> pager -> display($pager_url, false);
            echo $mv -> products -> pager -> displayNextLink("next", $pager_url);


Direct Queries