Pagination in PEAR and Smarty

First of all, you need to install PEAR in your web server. Then you need to install Smarty.

After that, you need to install a package in PEAR. It’s called ‘Pager’.

Pager is a class to page an array of data. It is taken as input and it is paged according to various parameters. Pager also builds links within a specified range, and allows complete customization of the output.

In the index.php file, we make a function named getPager(). This function contains four parameters – $item, $perPage, $mode, and $delta.

$item is the array of data.
$perPage is the number of data to be displayed per page.
$mode defines about the mode of pagination links. There are two types of modes – Sliding and Jumping. The Jumping mode displays ‘Back’ and ‘Next’ link.
$delta defines the width of pagination links. High value of $delta increases the width of pagination links.

The getPager() function returns an array of data and links. The first element of the array contains array of data and the second element contains pagination links.

index.php


<?php
require_once('classes/ConnectSmarty.class.php');
require_once ('Pager/Pager.php');

// create an object of the class included above
$smarty = new ConnectSmarty;

function getPager($item,$perPage,$mode,$delta)
{
    // setting Sliding as default mode
    if($mode == "")
    {
        $mode = 'Sliding';
    }

    // setting the default delta value as 2
    // delta means the width of pagination
    // more delta value results more width
    if($delta == "")
    {
    	$delta = '2';
    }

    $params = array(
		'mode'=>$mode,
		'perPage'=>$perPage,
		'delta'=>$delta,
		'itemData'=>$item
                );
				
    $pager =& Pager::factory($params);
    $data = $pager->getPageData();
    $links = $pager->getLinks();
    $links = $pager->links;
	
    return array($data,$links);
}

$mode = 'Sliding'; // $mode = 'Jumping';
$perPage = '3';
$delta = '2';
$item = array(
	    array('country_id'=>'1','country_name'=>'India'),
	    array('country_id'=>'2','country_name'=>'Pakistan'),
	    array('country_id'=>'3','country_name'=>'Nepal'),
	    array('country_id'=>'4','country_name'=>'Srilanka'),
	    array('country_id'=>'5','country_name'=>'Bangladesh'),
	    array('country_id'=>'6','country_name'=>'Maldives'),
	    array('country_id'=>'7','country_name'=>'Bhutan'),
	    array('country_id'=>'8','country_name'=>'Afganistan'),
	    array('country_id'=>'9','country_name'=>'China')
	);
		
$pagination = getPager($item,$perPage,$mode,$delta);
// print_r($pagination);

$smarty->assign('pager',$pagination[1]);
$smarty->assign('country',$pagination['0']);

// display the content
$smarty->display('index.tpl');
?>

foreach loop is used to display data. Before trying foreach loop, I had tried section loop but it didn’t work with pagination/pager. Smarty function ‘cycle’ is used to alternate row color of the table on which data is being displayed.

index.tpl


<html>
<head>
<title> Homepage : Pagination</title>
</head></p>
<body>

<table>
<tr style="background-color:#336699;color:#eeeeee">
<td>Country ID</td><td width="65%">Country Name</td>

{foreach from=$country item=view}
<tr bgcolor="{cycle values="#ffffff,#eeeeee"}">
<td align="center">{$view.country_id}</td>
<td>{$view.country_name}</td>
</tr>
{/foreach}

<tr><td colspan="5" align="center">{$pager}</td></tr>
</table>

</body>

</html>

Download source code