With the combined effort of my PHP Array Pagination script and this tutorial you will be able to pagination results (database & non database driven results) and intergrate it easily with the smarty template engine.
So, to start download the PHP Pagination script and then put it into a directory within your smarty setup.
During this example i am going to be using some test data, a basic PHP array with 10 sub array's, like so:
$myArray = array(
array('id' = > 1, 'title' => 'Test Item 1'),
array('id' = > 2, 'title' => 'Test Item 2'),
array('id' = > 3, 'title' => 'Test Item 3'),
array('id' = > 4, 'title' => 'Test Item 4'),
array('id' = > 5, 'title' => 'Test Item 5'),
array('id' = > 6, 'title' => 'Test Item 6'),
array('id' = > 7, 'title' => 'Test Item 7'),
array('id' = > 8, 'title' => 'Test Item 8'),
array('id' = > 9, 'title' => 'Test Item 9'),
array('id' = > 10, 'title' => 'Test Item 10'),
);
Then assign the pagination class to an object called pagination and assign the array and the amount of page numbers and assign this information into the smarty template vars, one for the listing of the items and another for the actual pagination.
$pagination = new pagination();
$myArray = $pagination->generate($myArray, 10);
$smarty->assign('listing', $myArray);
$smarty->assign('pagination', $pagination->links());
Then go into the template side of your setup and add the following code
{if !empty($listing)}
{if !empty($pagination)}
<div class="pagination">{$pagination}</div>
{/if}
{foreach item="item" from="$listing"}
<tr>
<td>{$item.id}</td>
<td>{$item.title}</td>
{/foreach}
{/if}
It's a pretty quick and simple solution to get you going as a beginner, good luck!
One of the main issues i found with using the smarty template engine is that there isn't enough documentation online!

March 27th, 2008 at 9:06 am
How would i go about using this with a mysql solution and not an array?
March 29th, 2008 at 12:43 am
Take a look at my PHP version of the script ...
http://www.lotsofcode.com/php/php-array-pagination.htm
March 29th, 2008 at 12:44 am
Pretty simple really, just add the result array into the pagination, like so:
< ?
$dataCollection = array();
$getData = mysql_query('SELECT * FROM tablename');
if ($rowData = mysql_fetch_assoc($getData)) {
do {
$dataCollection[] = $rowData;
} while($rowData = mysql_fetch_assoc($getData));
}
?>
Then parse it into the pagination object like so:
// Include the pagination class
include 'pagination.class.php';
// Create the pagination object
$pagination = new pagination;
// parse the data
$dataPages = $pagination->generate($rowData, 20);
foreach ($dataPages as $dataKey => $dataArray) {
// Show the information about the item
echo '
'.$dataArray['fieldname'].' '.$dataArray['fieldname2'].'
';
}
I hope that helps, it's pretty simple really ... !
March 31st, 2008 at 8:49 am
I have followed your example but does not seem to be loading any results.
PHP
$dataCollection = array();
$getData = mysql_query('SELECT * FROM users');
if ($rowData = mysql_fetch_assoc($getData)) {
do {
$dataCollection[] = $rowData;
} while($rowData = mysql_fetch_assoc($getData));
}
$pagination = new pagination();
$dataPages = $pagination->generate($rowData, 10);
$smarty->assign('listing', $dataPages);
$smarty->assign('pagination', $pagination->links());
TPL
{if !empty($listing)}
{if !empty($pagination)}
{$pagination}
{/if}
{foreach item="item" from="$listing"} {$item.username}
{/foreach}
{/if}
March 31st, 2008 at 9:47 am
Hi, have you included the `pagination.class.php`
August 6th, 2008 at 9:53 am
thanks this was really useful...
can I ask how do u apply search or filters?
such as searching only a user, yes does seach but after clicking next or other pages the filter is changed... other usernames are mixed after next..
August 7th, 2008 at 7:18 am
How do you apply filters?
October 28th, 2008 at 7:17 pm
Hey, it works if my query returns many results. But I get this error when my query returns 1 result:
Warning: array_slice() expects parameter 1 to be array, boolean given in /place/pagination.class.php on line 55
Help =(
November 17th, 2008 at 6:39 pm
Please help me , I need to list hundereds of users.
when i use your code i got this error and no output
Warning: array_slice() [function.array-slice]: The first argument should be an array in D:\NEW SOFT DONT DELETE\wamp\www\onlineexam\include\pagination.class.php on line 31
April 3rd, 2009 at 1:13 am
Thanks! Needed some simple paging working on Smarty.
Came across your blog post and implemented your PHP pagination class.
Works great!