?
Solved

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

Posted on 2005-03-09
4
Medium Priority
?
870 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 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