Paging and Limiting

Table of Contents

Determining the Current Page Number and Results Limit

One of the primary uses of container rules is to display lists of a data. If this is what your custom container rule does, you may want to split up the results into a series of pages.

If you want to enable this functionality, add the following code to your driver. Returning true from second method means the web site builder can choose to allow end-users to override the default limit via a URL parameter.

Example 5.1. Enabling paging and limiting of results

<?php
    class Driver_containers_rule_weather_controller extends Module_Containers_Driver_Abstract
    {
        // ...
        
        public function canBeMultiPaged()
        {
            return true;
        }

        public function limitCanBeSetInUrl()
        {
            return true;
        }
        
        // ...
    }
?>

Adding this code now means when the adds the container rule they can choose whether or not to split up the results. The user can then choose the number of results to display per page, and whether or not the page number and/or limit can be defined by the end-user in the URL.

Recite CMS will then determine the correct values that you can use to fetch your data.

Determining the Current Page Number and Results Limit

If you need to know the current page number when the container rule is running (that is, inside the run() method), call the getPageNumber() method. Pass to it the page request tree that is passed to run(). This will return an integer greater than or equal to 1.

Similarly, you can call getPageLimit() to determine the maximum number of results to return. This will return an integer greater than or equal to 0. If the value is zero, then there is no limit.

If required, you can then determine the offset using the formula (page - 1) * limit.

The following listing demonstrates this functionality, and how you might use it when building an SQL query.

Example 5.2. Determining the page number and results limit and using them in an SQL query

<?php
    class Driver_containers_rule_weather_controller extends Module_Containers_Driver_Abstract
    {
        // ...
        
        public function run(Module_Pages_ClientRenderer_PageRequestTree $pageRequestTree)
        {
            $page   = $this->getPageNumber($pageRequestTree);
            $limit  = $this->getPageLimit($pageRequestTree);
            $offset = ($page - 1) * $limit;
            
            $db = Application::GetDb();
            $select = $db->select();
            /* build the select */
            $select->limit($limit, $offset);
            
            /* perform query and use data */
        }
        
        // ...
    }
?>