The DBTNG layer provides developers with the ability to extend the query functionality for Select statements. There are two query extensions that are shipped with core. These allow you to easily page records and enable users to easily sort data displayed in tables. We will look at each in detail next.
In Drupal 6, paging was done using a pager_query
. In Drupal 7, the easiest way to add pagination to your query is using the PagerDefault
query extender. This takes care of automatically loading the current page from the page request to properly display the results for the current page. The extender can be added to the query by calling the extend
method:
<?php $query = db_select('node', 'n'); $query->addField('n', array('nid', 'title')); $query = $query->extend('PagerDefault')->limit(20); $result = $query->execute(); ?>
There are a couple of things to note in the previous code. After calling the extend
method, you should make sure...