Getting started


Views and routes


SQL queries

Sessions and security




To divide the long list of records on pages in any regular model you need to create an object of Pager class. To do this call "runPager($total, $limit)" method, 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 "countRecords()" method, and also we need to know at this moment which page of the list we are located at. For such purpose we use "defineCurrentPage($start_key)" method in "$mv -> router" object.

Example of template content "view-events.php" showing the main list of events

//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 active page in pagination, 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 URLs like "/news/?page=3"
$current_page = $mv -> router -> defineCurrentPage("page");

This way we can now add pagination into display function in events model. More details about records 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 initial page URL to which a number of 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 of records divided on limit per a page
  • getPage() - number of current page
  • display($path, $smart [, $extra_params]) - display a list of pages links (described above)
  • displayLimits($limits, $path [, $options]) - display a list of links with GET parameters as "pager-limit=10", $limits - one-dimensional array of integer numbers, $path – starting URL, to which GET parameters are added to. 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 "&" if needed. It's usually 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 per page :
        <? echo $mv -> products -> pager -> displayLimits($limits, $pager_url); ?>
    <? if($mv -> products -> pager -> hasPages()): ?>
    <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);
    <? endif; ?>


Direct queries