How to write a drupal pager_query? An example would be helpful?
I checked our the API function, but that didn't help.
<?php
//http://api.drupal.org/api/function/pager_query/6 says:
4.6 – 6
pager_query($query, $limit = 10, $element = 0, $count_query = NULL)
/**
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.
*/
?>
Refer drupal pager_query.
<?php
$list[]=array('');
$rows[] = array('');
$header = array(array('data'=>t('Title'),'field'=>'n.title'), array('data'=>t('Referral Author'),'field'=>'n.uid'), array('data'=>t('Time Created'),'field'=>'n.created', 'sort' => 'asc'), 'Average Rating');
$res = "SELECT n.title,n.nid, n.uid, n.created from {content_type_referral} cr join {node} n on cr.nid=n.nid and cr.vid=n.vid where n.type='referral' and n.status=1 ".tablesort_sql($header);
$count_query = "SELECT count(*) from {content_type_referral} s join {node} n on s.nid=n.nid and s.vid=n.vid";
$result_set = pager_query($res, 10, 0, $count_query);
while ($obj = db_fetch_object($result_set)){
$rows[] = array(l($obj->title, "node/".$obj->nid), $obj->uid, strftime ("%A, %d. %B %Y", $obj->created));
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 10, 0);
print $output;
?>
Post your Answer