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!
July 8th, 2009 at 12:09 pm
PLEASE TELL HOW TO CHANGE THE PAGINATION DISPLAY
OPTION I WANT TO USE IN SAMRTY TEMPLATES ACCORDING TO MY
DESIGN
July 8th, 2009 at 12:10 pm
PLEASE REPLY ITS URGENT I AM WAITING
July 8th, 2009 at 12:15 pm
my navigation bar is like this please help me your code really so simple but i cant modife according to my templates in smarty
Displaying 1 to 10 of Products prev(image) Jump to page dropdownbox (whioch show pages) of 1 Show Products per page textbox(To change the number of records per page)
September 14th, 2009 at 11:20 am
I have not seen such a worst pagination in my life. It increases the links [1 2 3 4 ....] as your result gows up. Is there any option so that one can shows only 10 or 12 numbers in a page.
September 18th, 2009 at 11:57 am
Great 1st class pagination class libarary!
Greetings from Poland
Mariusz
September 18th, 2009 at 1:41 pm
# 2009-09-18 MPI added multinational support
var $lang = 'en'; # default is English (but may be pl
var $Lang = array (
'en' => array ('first','previous','next','last'),
'pl' => array ('pierwsza','poprzednia','nastepna','ostatnia')
);
# $Lang['en'][0] returns 'first' $Lang['en'][1] returns 'previous'
# $Lang['en'][2] returns 'next' $Lang['en'][3] returns 'last'
function __construct ($lng = 'en')
{
$this->lang = $lng;
}
# in method links () replace from
#$plinks[]=' page-1).$queryURL.'">« Prev ';
# to
$plinks[] = ' page - 1).$queryURL.'">« '.$this->Lang[$this->lang][1].' ';
#
#
# pass language id to class constructor
$pg = new pagination ('pl');
September 27th, 2009 at 6:01 pm
I downloaded and used pagination.class.php to paginate a multi tabbed blogging site. The pagination shows in all tabs and not correctly. How do I individually paginate each tabs or fragments of the page? I am a self taught php programmer. Please post the cirrected code. Thanks!
function isClear()
{
document.myForm[0].value='';
return true;
}
<?php
ob_start();
if(!empty($_SESSION['userID']) and !empty($_POST['user'])){
echo '
';
}
?>
Hello world is blah, blah, blah.......!
Chat, news and more
Links
Images
<?php
$sql = mysql_query("SELECT users.userUser, posts.postID, posts.postPost, posts.postTime, posts.postComments, userAvatar FROM users, posts WHERE users.userID = posts.postUserID ORDER BY posts.postTime DESC ");
while($row = mysql_fetch_array($sql)){
$abcd= $row['userName'];
$abcd = str_replace (" ", "-", $abcd);
$time = reltime($row['postTime']);
echo '
'.$row['postPost'].'Posted by: '.$row['userUser'].', Comments '.$row['postComments'].' , Time elapsed: '.$time.'
';}
?>
Prev Next
var gallery=new virtualpaginate("virtualpage",10)
gallery.buildpagination("gallerypaginate")
<?php
$sql = mysql_query("SELECT users.userUser, links.linksID, links.linksLink, links.linksPost, links.linksTime, links.linksComments,userAvatar FROM users, links WHERE users.userID = links.linksUserID ORDER BY links.linksTime DESC");
while($row = mysql_fetch_array($sql)){
$abcd= $row['userName'];
$abcd = str_replace (" ", "-", $abcd);
$time1 = reltime($row['linksTime']);
echo '
'.$row['linksPost'].'Posted by: '.$row['userUser'].', Comments '.$row['linksComments'].' , Time elapsed: '.$time1.'
';}
?>
September 27th, 2009 at 6:15 pm
I need to re-post as some codes didn't show up .I had downloaded and used pagination.class.php to paginate a multi tabbed blogging site. The pagination shows in all tabs and not correctly. How do I individually paginate each tabs or fragments of the page? I am a self taught php programmer. Please post the cirrected code. Thanks!
[php]
// database connection
require_once('./includes/base.php');
include 'pagination.class.php';
if($_GET['logout'] == 1){
@$_SESSION = array();
@session_unregister($_SESSION['userID']);
@session_unregister($_SESSION['userName']);
@session_unregister($_SESSION['userUser']);
@session_unregister($_SESSION['userPass']);
@session_destroy();
}
require("./templates/$template/header.php");
[/php]
function isClear()
{
document.myForm[0].value='';
return true;
}
[php]
ob_start();
if(!empty($_SESSION['userID']) and !empty($_POST['user'])){
echo '
';
}
[/php]
Hello world [php]echo "$sitename"; [/php] is blah, blah, blah.......!
Blog
Links
Images
[php]
$sql = mysql_query("SELECT users.userUser, posts.postID, posts.postPost, posts.postTime, posts.postComments, userAvatar FROM users, posts WHERE users.userID = posts.postUserID ORDER BY posts.postTime DESC ");
while($row = mysql_fetch_array($sql)){
$abcd= $row['userName'];
$abcd = str_replace (" ", "-", $abcd);
$time = reltime($row['postTime']);
echo '
'.$row['postPost'].'Posted by: '.$row['userUser'].', Comments '.$row['postComments'].' , Time elapsed: '.$time.'
';}
[/php]
Prev Next
var gallery=new virtualpaginate("virtualpage",10)
gallery.buildpagination("gallerypaginate")
[php]
$sql = mysql_query("SELECT users.userUser, links.linksID, links.linksLink, links.linksPost, links.linksTime, links.linksComments,userAvatar FROM users, links WHERE users.userID = links.linksUserID ORDER BY links.linksTime DESC");
while($row = mysql_fetch_array($sql)){
$abcd= $row['userName'];
$abcd = str_replace (" ", "-", $abcd);
$time1 = reltime($row['linksTime']);
echo '
'.$row['linksPost'].'Posted by: '.$row['userUser'].', Comments '.$row['linksComments'].' , Time elapsed: '.$time1.'
';}
[/php]
[php]
// testing
grabimages('SELECT users.userUser, images.imagesID, images.imagesFile, images.imagesPost, images.imagesTime, images.imagesComments FROM users, images WHERE users.userID = images.imagesUserID ORDER BY images.imagesTime DESC LIMIT 20');
[/php]