List with Pagination ==================== Listings with pagination are required in most of applications. PHPLucidFrame provides two ways of creating listings - with AJAX and without AJAX. There are two configuration variables in ``/app/inc/site.config.php`` which will be used here. :: # $lc_pageNumLimit: number of page numbers to be shown in pager $lc_pageNumLimit = 10; # $lc_itemsPerPage: number of items per page in pager $lc_itemsPerPage = 15; For every grow-in-size and data-centric web application, displaying a reasonable number of records per page has always been a crucial part. PHPLucidFrame provides a quick, easy and handy way to paginate data to cure headaches of developers. LucidFrame offers a class Pager for pagination to make building paginated queries easier. We start by getting the number of total records which is expected by the class Pager. :: # Count query for the pager $totalCount = db_count('post') ->where()->condition('deleted', null) ->fetch(); Once we retrieved the number of total records, we can create an instance from the Pager class. There is a helper function `_pager()` to create a Pager instance. By default, the field name ``page`` is used in query string for the current page number. We can customize it by passing the name to the function such as ``_pager('p')``. The instance looks for the number of records per page ``$lc_itemsPerPage`` and the number of page limit ``$lc_pageNumLimit``. The total number of records has to be set to the instance as well. If we use images for navigation arrows, we can set the image directory path to the ``imagePath`` property of the instance, but we can also make them from CSS. To incorporate AJAX functionality into pagination, you can make it easily by setting the ajax property to true. The ``calculate()`` method has to be invoked to calculate offset. :: # Prerequisite for the Pager $pager = _pager() ->set('itemsPerPage', _cfg('itemsPerPage')) // $lc_itemsPerPage ->set('pageNumLimit', _cfg('pageNumLimit')) // $lc_pageNumLimit ->set('total', $totalCount) // the total record count fetched earlier ->set('ajax', true) // flag as AJAX-enabled list ->set('imagePath', WEB_ROOT . 'images/pager/') // optional; if you use images for pagination ->calculate() // required to calculate offset However, you can minimize your code when you don't need to customize your pagination settings. The following one-line is equivalent to a couple of lines aforementioned. :: # Prerequisite for the Pager $pager = pager_ajax($totalCount); Then, we can use ``$pager->get('offset')`` and ``$pager->get('itemsPerPage')`` in our query ``LIMIT`` clause. :: $qb = db_select('post', 'p') ->where()->condition('p.deleted', null) ->orderBy('p.created', 'DESC') ->limit($pager->get('offset'), $pager->get('itemsPerPage')); Finally, we call ``$pager->display()`` where we want to appear the pager. By default, the pager will be displayed using ```` tag, however, we can easily change it to ``
1 2
If we use ``imagePath``, the output HTML will be generated with ```` tag. The following images have to be available in our ``imagePath``: #. end.png #. end_disabled.png #. next.png #. next_disabled.png #. previous.png #. previous_disabled.png #. start.png #. start_disabled.png ::
1 2
If we use ``$pager->set('htmlTag', '