pager_query

Versions
4.6 – 6
pager_query($query, $limit = 10, $element = 0, $count_query = NULL)

Perform a paged database query.

Use this function when doing select queries you wish to be able to page. The pager uses LIMIT-based queries to fetch only the records required to render a certain page. However, it has to learn the total number of records returned by the query to compute the number of pages (the number of records / records per page). This is done by inserting "COUNT(*)" in the original query. For example, the query "SELECT nid, type FROM node WHERE status = '1' ORDER BY sticky DESC, created DESC" would be rewritten to read "SELECT COUNT(*) FROM node WHERE status = '1' ORDER BY sticky DESC, created DESC". Rewriting the query is accomplished using a regular expression.

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 parameter.

Parameters

$query The SQL query that needs paging.

$limit The number of query results to display per page.

$element An optional integer to distinguish between multiple pagers on one page.

$count_query An SQL query used to count matching records.

... A variable number of arguments which are substituted into the query (and the count query) using printf() syntax. Instead of a variable number of query arguments, you may also pass a single array containing the query arguments.

Return value

A database query result resource, or FALSE if the query was not executed correctly.

Related topics

▾ 25 functions call pager_query()

blog_page_last in modules/blog.module
Displays a Drupal page containing recent blog entries of all users.
blog_page_user in modules/blog.module
Displays a Drupal page containing recent blog entries of a given user.
comment_admin_overview in modules/comment.module
Menu callback; present an administrative comment listing.
comment_render in modules/comment.module
do_search in modules/search.module
Do a query on the full-text search index for a word or words.
forum_get_topics in modules/forum.module
node_admin_nodes in modules/node.module
Generate the content administration overview.
node_page_default in modules/node.module
Generate a listing of promoted nodes.
path_overview in modules/path.module
Return a listing of all defined URL aliases.
poll_page in modules/poll.module
profile_browse in modules/profile.module
Menu callback; display a list of user information.
queue_overview in modules/queue.module
Display a page listing the nodes in the submission queue.
statistics_node_tracker in modules/statistics.module
statistics_recent_hits in modules/statistics.module
Menu callback; presents the "Recent hits" page.
statistics_top_pages in modules/statistics.module
Menu callback; presents the "Top pages" page.
statistics_top_referrers in modules/statistics.module
Menu callback; presents the "Top referrers" page.
statistics_top_users in modules/statistics.module
Menu callback; presents the "Top users" page.
statistics_user_tracker in modules/statistics.module
taxonomy_select_nodes in modules/taxonomy.module
Finds all nodes that match selected taxonomy conditions.
tracker_page in modules/tracker.module
Menu callback. Prints a listing of active nodes on the site.
user_admin_account in modules/user.module
user_search in modules/user.module
Implementation of hook_search().
watchdog_overview in modules/watchdog.module
Menu callback; displays a listing of log messages.
_aggregator_page_list in modules/aggregator.module
Prints an aggregator page listing a number of feed items. Various menu callbacks use this function to print their feeds.
_locale_string_seek in includes/locale.inc
Perform a string search and display results in a table

Code

includes/pager.inc, line 50

<?php
function pager_query($query, $limit = 10, $element = 0, $count_query = NULL) {
  global $pager_from_array, $pager_total;
  $from = $_GET['from'];

  // Substitute in query arguments.
  $args = func_get_args();
  $args = array_slice($args, 4);
  // Alternative syntax for '...'
  if (is_array($args[0])) {
    $args = $args[0];
  }

  // Construct a count query if none was given.
  if (!isset($count_query)) {
    $count_query = preg_replace(array('/SELECT.*?FROM/As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM', ''), $query);
  }

  // Convert comma-separated $from to an array, used by other functions.
  $pager_from_array = explode(',', $from);

  if (count($args)) {
    $pager_total[$element] = db_result(db_query($count_query, $args));
    return db_query_range($query, $args, (int)$pager_from_array[$element], (int)$limit);
  }
  else {
    $pager_total[$element] = db_result(db_query($count_query));
    return db_query_range($query, (int)$pager_from_array[$element], (int)$limit);
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.