?
Solved

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

Posted on 2005-03-09
4
Medium Priority
?
875 Views
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:

site.com/index.php?a=members&st=25

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
0
Comment
Question by:MaybeItsJeremy
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

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

session_start();
require('Smarty.class.php');
require('SmartyPaginate.class.php');
   
$smarty =& new Smarty;
// required connect
SmartyPaginate::connect();
// set items per page
SmartyPaginate::setLimit(25);
SmartyPaginate::setUrlVar('st')
$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
SmartyPaginate::assign($smarty);
// display results
$smarty->display('index.tpl');

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

Author Comment

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

SmartyPaginate::connect();
SmartyPaginate::setLimit(2);
SmartyPaginate::setUrlVar('st');

$_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
SmartyPaginate::assign($admin);
// 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.
0
 
LVL 25

Expert Comment

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

var_dump($data);

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

Author Comment

by:MaybeItsJeremy
ID: 13551089
Got it working.... Thanks.
0

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