How to create pagination in Drupal 7 db queries.

There are two things you need when building your query.
1. The 'PagerDefault' extend
2. The pager theme in your returned array.

<?php
function example_pager(){
  
$select = db_select('node','n')
     ->
fields('n', array('nid')
     ->
condition('status', 1)
     ->
orderBy('created', 'DESC')
     ->
extend('PagerDefault')
     ->
limit(10);

  

$nids = $select->execute();
  
   if(!empty(
$nids)){
    
$nodes = node_load_multiple($nids);
    
$build = node_view_multiple($nodes);

    

$build['pager'] = array(
       
'#theme' => 'pager',
       
'#weight' => 5
    
);

   }
   return

$build;
}
?>
ABOUT THE AUTHOR
Lead Developer
0 Comment(s) to the "How to create pagination in Drupal 7 db queries. "
  • Services
  • Latest
  • Tags

Netrift Services

If you need high quality design services for brochure websites or content management systems, you are in the right place.

Custom Drupal Modules
PHP Programming
Website Debugging
Facebook Apps
Business Intranet sites.
1-on-1 Drupal tutorials.

Welcome to the drupal realestate module. This is a FREE modu...
Wed, 2012-01-25 15:23
Lets assume you have a custom module that display is own cus...
Tue, 2011-04-26 14:08
One of my favorite modules to install. As developers, we nee...
Wed, 2011-04-13 09:40
This is a small personal project geared towards showing the...
Tue, 2011-04-12 15:35