Flipkart

Wednesday, March 10, 2010

Drupal Pagination using pager_query()

This is the simplest pagination method using pager theme in Drupal, we can customize the links using pagination.inc file in includes folder also.

$limit = 7;
$query = "select nid,title from {node} where type='event'";
$count = "select count(*) from {node} where type='event'";

  $result = pager_query($query, $limit , 0, $sql_count);
  $output .= theme('pager', NULL, $items_per_page, 0);


GROUP BY

Queries with GROUP BY clauses need special care. Copied from the documentation on pager_query:

Unfortunately, the rewrite rule does not always work as intended for queries that already have a "COUNT(*)" or a "GROUP BY" clause, and possibly for other complex queries. In those cases, you can optionally pass a query that will be used to count the records. For example, if you want to page the query "SELECT COUNT(*), TYPE FROM node GROUP BY TYPE", pager_query() would invoke the incorrect query "SELECT COUNT(*) FROM node GROUP BY TYPE". So instead, you should pass "SELECT COUNT(DISTINCT(TYPE)) FROM node" as the optional $count_query [fourth] parameter.

No comments:

Post a Comment