Lorenzo Alberton

London, UK   ·   Contact me   ·  

« Articles

PEAR::Pager Tutorials - Use Pager with Smarty. Use Pager_Wrapper with AJAX

Abstract: Since I keep getting emails asking for help with PEAR::Pager, here's a couple of new examples about the most requested features: 1) how to use Pager with a template engine like Smarty; 2) how to use Pager_Wrapper to paginate database results and display them using AJAX.

Pager and...

I think I've already talked about all the elements you may ever need to effectively use PEAR::Pager in any configuration, but I keep on receiving a lot of mails asking how to use Pager with this and with that to do something. The two top questions are: a) "How can I use Pager with [template engine]?" and b) "How can I use Pager_Wrapper with AJAX?". In the hope of reducing the flow of mails, I'm going to show you two full examples.

Pager and Smarty

The following examples uses the popular Smarty template engine, but you should be able to use any template engine. First, we create a Pager instance, then we assign the paged data and the links to some template variables:

require_once 'Pager.php';

// array of items to paginate
$items = array(...);

$pagerOptions = array(
    'mode'     => 'Sliding',
    'delta'    => 3,
    'perPage'  => 10,
    'itemData' => $items,
$pager =& Pager::factory($pagerOptions);

//fetch the paged data into the $data variable
$data = $pager->getPageData();
if (!is_array($data)) {
    $data = array();

//assume you have an instantiated Smarty object into the $smarty variable

//assign the paged data and the links to template variables
$smarty->assign('items', $data);
$smarty->assign('pager_links', $pager->links);
    'page_numbers', array(
        'current' => $pager->getCurrentPageID(),
        'total'   => $pager->numPages()


And here's the template:


{if $page_numbers.total > 1}
(page {$page_numbers.current} / {$page_numbers.total})<br />

{foreach item=row from=$items}


As you can see, there's nothing difficult about it. Plain and clear.

Pager_Wrapper and AJAX

The second example shows how to use Pager_Wrapper to paginate database results and HTML_AJAX to display them. The html file is identical to the one I've already shown in the Pager + AJAX tutorial:

<h1>PEAR::Pager example with AJAX</h1>

<script type="text/javascript" src="server.php?client=all"></script>

<div id="target">I'm the target</div>

<script type="text/javascript">
    HTML_AJAX.replace('target', 'testdata.php');


The testdata.php file is a simple php script where you fetch the data from the database with the help of Pager_Wrapper. The output of this script will replace the contents of the target div in the first html file. We also print the current datetime to prove the data is "fresh" and built at the time of each call (i.e. every time you click on a navigation link).

//copy the Pager_Wrapper file where you can include it
require_once 'Pager_Wrapper.php';
require_once 'MDB2.php';

//skipped the db connection code...
//let's just suppose we have a valid db connection in $db.

$pager_params = array(
    'mode'     => 'Sliding',
    'append'   => false,  //don't append the GET parameters to the url
    'path'     => '',
    'fileName' => 'javascript:HTML_AJAX.replace(\'target\',\'testdata.php?pageID=%d\');',  //Pager replaces "%d" with the page number...
    'perPage'  => 10, //show 10 items per page
    'delta'    => 1,
    'itemData' => $data,
$query = 'SELECT prod_name, prod_description FROM products';

$paged_data = Pager_Wrapper_MDB2($db, $query, $pager_params);
if (PEAR::isError($paged_data)) {
    //uh-oh... something went wrong. Deal with it.

//show the results
echo '<p>This container is loaded with an AJAX call</p>';
echo '<p><span class="datetime">DateTime: '. date('Y-m-d H:i:s') .'</span></p>';
echo '<h3>Page '. $paged_data['page_numbers']['current'] .'/'. $paged_data['page_numbers']['total'].'</h3>';
foreach ($paged_data['data'] as $item) {
    echo '<strong>'.$item['prod_name'].'</strong>: '. $item['prod_description'] .'<br />';

//show the links
echo '<hr />'.paged_data['links'];

See, it's almost the same example as it was described here, but with some changes taken from here. HTH.

« Go back to PEAR::Pager tutorials index.

1 response to "PEAR::Pager Tutorials - Use Pager with Smarty. Use Pager_Wrapper with AJAX"

Related articles

Latest articles

Filter articles by topic

AJAX, Apache, Book Review, Charset, Cheat Sheet, Data structures, Database, Firebird SQL, Hadoop, Imagick, INFORMATION_SCHEMA, JavaScript, Kafka, Linux, Message Queues, mod_rewrite, Monitoring, MySQL, NoSQL, Oracle, PDO, PEAR, Performance, PHP, PostgreSQL, Profiling, Scalability, Security, SPL, SQL Server, SQLite, Testing, Tutorial, TYPO3, Windows, Zend Framework