SmartyPaginate: How the heck can I get it to work?

Posted on 2005-03-09
Medium Priority
Last Modified: 2013-12-12
Using a MySQL query with start limits definied via the URI, how can I get SmartyPaginate to work for me?I'm just not sure how to get and then set the totals from the query, etc....

At the moment, I am attempting to use this to paginate my member listing, so the variable "st" in the URI defines which row to start at, for example:


That would start after the 25th row, and stop after the next 25. The SmartyPaginate would show (obviously after my choices in the template):

5 Pages: Last Page | Previous Page |1 2 [3] 4 5 | Next Page | Last Page
Question by:MaybeItsJeremy
  • 2
  • 2
LVL 25

Accepted Solution

Marcus Bointon earned 2000 total points
ID: 13504459
Going from the docs at: http://www.phpinsider.com/php/code/SmartyPaginate/

$smarty =& new Smarty;
// required connect
// set items per page
$data = array();
$count = 0;
if ($res = mysql_query('SELECT SQL_CALC_FOUND_ROWS, * FROM mytable LIMIT '.SmartyPaginate::getCurrentIndex().', '.SmartyPaginate::getLimit())) {
while ($data[] = mysql_fetch_array($res));
SmartyPaginate::setTotal($data[0][0]); //this will contain the total number in the found set that would have been found without the limit clause
// assign your db results to the template
$smarty->assign('results', $data);
// assign {$paginate} var
// display results

It's now up to you what you do in the template.

I've not used SmartyPaginate before (I normally use PEAR::Pager_Sliding); It seems quite complex, and very much in a PHP5 style, looks like it's relying very much on static class vars. The docs could do with a real DB example like this one, which I should mention is untested, but I think I've ticked all the right boxes.

Author Comment

ID: 13512778
I cleaned up your code a bit.... I have this:


$_data = array();
$count = 0;

if ($res = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM se_macros LIMIT ".SmartyPaginate::getCurrentIndex().", ".SmartyPaginate::getLimit().";")) {
while ($_data[] = mysql_fetch_array($res));
SmartyPaginate::setTotal($_data[0][0]); //this will contain the total number in the found set that would have been found without the limit clause

// assign your db results to the template
$admin->assign('results', $_data);
// assign {$paginate} var
// display results

Just for testing I set the limit to 2. There are about 20 rows in my table, yet only one page shows up in the pagination.
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13515503
Just for sanity, make sure that the right data is really in the array:


and that SmartyPaginate::setTotal has actually worked. (is there a getTotal() method?)

Author Comment

ID: 13551089
Got it working.... Thanks.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question