Aug 24, 2010

Posted by in Drupal | 2 comments

Pagination in DRUPAL

I was really upset and irritated after searching a lot about having pagination in drupal. I was not getting any thing, but when I found the solution, I have cried, it was so simple and easy and the best. It can’t be any better than what it is.. really..

So here is how we can integrate / have pagination in drupal for any of our custom query.

Lets say for example, we want to have pagination for the following query, we would need 25 records per page.

Read the comments carefully

//the query should be a simple query with out any limit condition in it.

/*
Very IMP :: IF you are not passing the count query to the pager_query function, then you need to

make sure that the SQL Keywords in your quries are in CAPS. This is because, Drupal uses

preg_replace function to match those keywords in your query and replace it with a count query.

Example - SELECT, FROM, WHERE, ORDER BY should be in caps
*/


$sql = "SELECT * FROM comments WHERE condition";

/*
-Now execute the query to get result with limit
-Here 25 is the limit of records we want.

The following pager_query will return the resource object, it is exactly same as mysql_query, and
later we can use it to fetch the records..
*/


$queryResult = pager_query(db_rewrite_sql($sql), 25);

//OR - you can also pass your custom count query (if your SELECT query is much complex)

$count_query = "SELECT count(*) FROM tablename";

$queryResult = pager_query(db_rewrite_sql($sql), 25,0,$count_query);

/*
AS PER DRUPAL, Following are the parameters for pager_query function.

 * @param $query
 *   The SQL query that needs paging.
 * @param $limit
 *   The number of query results to display per page.
 * @param $element
 *   An optional integer to distinguish between multiple pagers on one page.
 * @param $count_query
 *   An SQL query used to count matching records.
 * @param ...
 *   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
 *   A database query result resource, or FALSE if the query was not executed
 *   correctly.
 
*/

Now in the theme file, i.e for ex in the tpl.php file we need to output the pagination links. Here is how we can do that.

/*
the following function call will show the pagination links, we can
call it in the tpl.php file or in any of the custom menu callback function.
*/


echo theme('pager', NULL, variable_get('default_nodes_main', 10));

It is very easy as you just need to do 2 function calls and get your desired output. If it’s still confusing for you, kindly comment your doubt and I will promptly reply them

.

NOTE :: You neednot bother about appending any search crieteria to the pagination links as it automatically takes care of it and appends any posted data. This is helpfull when you want to have pagination on a search result

Styling the pagination links

This is simple styling, you can add your own way of styling to the classes to make it more attractive.

/*pagination css*/
.item-list ul
{
margin:10px 0 0 0;
padding:0;
}

.item-list ul li
{
list-style:none;
float:left;
margin-right:5px;
}

.item-list ul li.pager-item a
{
font-weight:700;
color:#000;
}

.item-list ul li.pager-current
{
color:#F00;
font-weight:bold;
}
  1. Thank you, I’ve been searching for an easy explanation of this all day!

  2. Awesome! Thanks for this.:)

Leave a Reply