Monday 24th March 2008Simple Smarty Pagination
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!
How would i go about using this with a mysql solution and not an array?
March 27th, 2008 at 9:06 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:43 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 29th, 2008 at 12:44 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 8:49 am
Hi, have you included the `pagination.class.php`
March 31st, 2008 at 9:47 am