Home » PEAR, Smarty20 July 2008

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');</p>
// create an object of the class included above
$smarty = new ConnectSmarty;</p>
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. After that I tried the foreach loop and it worked fine and smooth. J

A custom Smarty function ‘cycle’ is used to alternate row color of the table on which data is being displayed. Cycle is used to cycle through a set of values. This makes it easy to alternate between two or more colors in a table, or cycle through an array of values.

index.tpl

<html></p>
<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

Note: I have not included Smarty library files in this zip file. You can download the Smarty library files from http://smarty.php.net/download.php or directly from http://chapagain.googlecode.com/files/smarty.zip

From Mukesh Chapagain's Blog, post Pagination in PEAR and Smarty

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • http://www.deepas-chitnchat.blogspot.com deepa

    Nice blog you have. You also most welcome to my world I mean to say my blog.

  • http://markmatanza.com Mark

    Hi Mukesh,

    Do you have a demo of this?

    Thanks,
    Mark

  • http://blog.chapagain.com.np Mukesh

    Sorry, I don’t have the demo. You can try it in your localhost.

  • Unknown

    i dont know why u provide only wrong code or incomplete code , it might be misguide everybody .i found your many code wrong or not complete if dont know your self dont misguide others.
    thanks

  • http://blog.chapagain.com.np/ Mukesh

    All of my code had been tested before posting. I have written this article a long time back. Not tested in the latest version of PEAR / Smarty. There might be some upgrade issue.

    One more thing, in most of my articles, I have tried to be as descriptive as possible. Also, providing full source code. I have never tried to misguide others. I started this blog so that I could help others in programming and my intension will always be the same.

    Thanks.