Rendering an AJAX Pager with Custom PHP

Dev

Rendering an AJAX Pager with Custom PHP

by Chris Armstrong

Drupal is a great tool, with its customizable templates. However, there are some things you just can’t seem to do with those .tpl.php files – and one of them seems to be rendering out an AJAX pager.

Background

For our new blog layout, we wanted to have a block of all the names and pictures of each blog author. Since we have some 30-40 contributors to the blog, we needed a way to use a pager, but not interfere with our main blog paging system and we wanted it to be AJAX, so there wasn’t a page refresh.
The issue here is the pager_query() function in Drupal doesn’t allow for an AJAX pager. So you can throw all the custom PHP and SQL functions you want into a .tpl.php file, and it still will require a page refresh.

The trick is to take your custom PHP into Views.

Requirements

  • Drupal 6+ (obviously)
  • Views
  • Views Custom Field
  • Your custom PHP/SQL (since this post is about rendering out the pager, I’m not as concerned with the functions and variables it takes to get there. However, you should have at least $limit and $sql variables with $result = pager_query($sql, $limit) where $limit is a number).

Putting It Together

Create a new View, then, create a new block.

Add a new field into your View, choose the “Customfield: PHP code”.

Custom Field

Paste your custom code into the field.

Custom PHP code

After that is saved, change “Use AJAX” to “Yes” and change “Items per Page” to “1”. These are important steps as you should have already set the limit on your $limit variable in the pager_query() function. If you set the Items per Page to more than 1, you’ll get that many sets of your function.

AJAX settings

Pager settings

To avoid conflicts with the main blog pager, I used Views’ built-in pager element numbering system, but I changed the main blog pager element to 1, instead of changing this AJAX pager element.

Main blog pager element change

After saving, you should have your block of pictures/names/whatever with an AJAX pager – just like the one to the right of this post!

More About the Author

Chris Armstrong

Multimedia Manager
Drupal Quick Tip: Auto “node/nid” Paths from Alias in Menus “In learning you will teach, and in teaching you will learn.” Words spoken by (or, at least, attributed to) the great Phil ...
Quick Tutorial on Overriding Drupal Views using Templates Scott and I were recently invited to Oklahoma State University to provide Drupal training for their web admins. Drupal has been on the ...

See more from this author →

Subscribe to our newsletter

  • I understand that InterWorks will use the data provided for the purpose of communication and the administration my request. InterWorks will never disclose or sell any personal data except where required to do so by law. Finally, I understand that future communications related topics and events may be sent from InterWorks, but I can opt-out at any time.
  • This field is for validation purposes and should be left unchanged.

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072