|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
__fabrice Smarty Rookie
Joined: 13 Jan 2005 Posts: 18
|
Posted: Thu Jan 13, 2005 1:24 pm Post subject: [SOLVED] SmartyPaginate, work with parameter $_GET[''] ? |
|
|
Hi,
I'm a problem with using SmartyPaginate, here my code :
Code: | $smarty =& new Smarty;
$smarty->template_dir = $path_to_smarty.'/templates/';
$smarty->compile_dir = $path_to_smarty.'/templates/';
$smarty->config_dir = $path_to_smarty.'/configs/';
$smarty->cache_dir = $path_to_smarty.'/cache/';
$smarty->plugin_dir = $path_to_smarty.'/plugins/';
SmartyPaginate::connect();
SmartyPaginate::setLimit(5);
$BDDsonneriesmobile = new class_mysql_db;
function get_db_results() {
$BDDsonneriesmobile = new class_mysql_db;
$query = "SELECT * FROM rubrique_sonnerie_poly ";
$query .= " ORDER BY texte ASC";
$BDDsonneriesmobile->query($query);
if ($BDDsonneriesmobile->num_rows()) {
$_data = $BDDsonneriesmobile->sql_query();
SmartyPaginate::setTotal(count($_data));
return array_slice($_data, SmartyPaginate::getCurrentItem(), SmartyPaginate::getLimit());
}
}
$smarty->assign('results', get_db_results());
SmartyPaginate::assign($smarty);
$smarty->display('paginate.tpl'); |
and the template : Code: | {section name="idx_sonnerie" loop=$results}
- {$results[idx_sonnerie].texte} <br>
{/section}
<br>
{* display pagination header *}
{$paginate.first}-{$paginate.last} sur {$paginate.total} titres :
{* display pagination info *}
{paginate_prev} {paginate_middle} {paginate_next} |
and, i think , it's work... but, i want to add and use a parameter $_GET with my query, like : "SELECT * FROM rubrique_sonnerie_poly AND id > 5", so the parameter is "next" and i can't add anymore ?
How can I do ?...
Thanks a lot, and escuse me for my "poor" english
Regards,
Fabrice
Last edited by __fabrice on Tue May 24, 2005 1:59 pm; edited 1 time in total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jan 13, 2005 3:25 pm Post subject: Re: SmartyPaginate, work with parameter $_GET[''] ? |
|
|
__fabrice wrote: |
$smarty->template_dir = $path_to_smarty.'/templates/';
$smarty->compile_dir = $path_to_smarty.'/templates/';
|
You probably don't want your template_dir and compile_dir to be the same directory, but that is besides the question
__fabrice wrote: |
function get_db_results() {
$BDDsonneriesmobile = new class_mysql_db;
$query = "SELECT * FROM rubrique_sonnerie_poly ";
$query .= " ORDER BY texte ASC";
$BDDsonneriesmobile->query($query);
if ($BDDsonneriesmobile->num_rows()) {
$_data = $BDDsonneriesmobile->sql_query();
SmartyPaginate::setTotal(count($_data));
return array_slice($_data, SmartyPaginate::getCurrentItem(), SmartyPaginate::getLimit());
}
|
Notice what is happening here, you are selecting all the rows from the database, the slicing the resulting array. A more efficient approach would be to select only the rows you need. Something like:
[php:1:cdf30ab1ac]
$index = SmartyPaginate::getCurrentIndex();
$limit = SmartyPaginate::getLimit();
$query = "SELECT * FROM rubrique_sonnerie_poly LIMIT $index,$limit"
[/php:1:cdf30ab1ac]
using getCurrentIndex() instead of getCurrentItem() should make your "next" parameter correct (?) |
|
Back to top |
|
__fabrice Smarty Rookie
Joined: 13 Jan 2005 Posts: 18
|
Posted: Thu Jan 13, 2005 4:11 pm Post subject: |
|
|
Thanks for help
If i try your code : Code: | $index = SmartyPaginate::getCurrentIndex();
$limit = SmartyPaginate::getLimit();
$query = "SELECT * FROM rubrique_sonnerie_poly LIMIT ". $index.",".$limit;
|
I have just the 5 first records (1 to 5), 'cause the "limit" is set. But, in my case, i want also the "navigationbar".
My code that i give is ok, but, i want to know if i could add a parameter, cause, with the "navigation bar", there's only the "next" parameter, and i need others.
Thanks a lot,
Fabrice from France |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jan 13, 2005 4:21 pm Post subject: |
|
|
For the navigation bar, you only need to know the total number of records there would be in the query without LIMIT. MySQL provides a way to calculate this without a double query. That is explained in the SmartyPaginate docs.
SELECT SQL_CALC_FOUND_ROWS * FROM mytable LIMIT X,Y
SELECT FOUND_ROWS() as total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jan 13, 2005 4:25 pm Post subject: |
|
|
__fabrice wrote: |
My code that i give is ok, but, i want to know if i could add a parameter, cause, with the "navigation bar", there's only the "next" parameter, and i need others.
|
If you want to add a custom dynamic parameter to the navigation URL, you will need to build your own navigation links instead of using the {paginate_*} functions. All the variables you need are available from {$paginate}. You could also modify the {paginate_*} funcs to suit your needs, maybe add an optional attribute. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jan 13, 2005 4:31 pm Post subject: |
|
|
You could also use SmartyPaginate::setURL() to set a new URL with your parameter(s) on each invocation. |
|
Back to top |
|
__fabrice Smarty Rookie
Joined: 13 Jan 2005 Posts: 18
|
Posted: Thu Jan 13, 2005 7:10 pm Post subject: |
|
|
Hi,
Thank you very much, for help.
I try to use your code :
Code: | $index = SmartyPaginate::getCurrentIndex();
$limit = SmartyPaginate::getLimit();
$query = "SELECT * FROM rubrique_sonnerie_poly LIMIT ". $index.",".$limit; |
But the result is not good, only the "LIMIT X,Y" records is returned (also in the navigation bar).
I dont' understand how and where to use "LIMIT" and having all the records in the navigation bar.
Regards,
Fabrice[/code] |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|