preload
Simple Modular Arithmetic with Smarty Template Engine Three Columns with Header, Menu and Footer
Mar 24

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!

10 Responses to “Simple Smarty Pagination”

  1. Paul Says:

    How would i go about using this with a mysql solution and not an array?

  2. lotsofcode Says:

    Take a look at my PHP version of the script ...

    http://www.lotsofcode.com/php/php-array-pagination.htm

  3. lotsofcode Says:

    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 ... !

  4. Paul Says:

    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}

  5. lotsofcode Says:

    Hi, have you included the `pagination.class.php`

  6. Allan Says:

    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..

  7. josh Says:

    How do you apply filters?

  8. Sarvar Says:

    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 =(

  9. manfred Says:

    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

  10. Ray Says:

    Thanks! Needed some simple paging working on Smarty.
    Came across your blog post and implemented your PHP pagination class.

    Works great!

Leave a Reply

 
 
Please enter the word above into the box below, the word is not case sensitive.
 
If you have trouble reading the word above then click here to load a new word.
Security word: