View previous topic :: View next topic |
Author |
Message |
tk Smarty Regular
Joined: 31 Oct 2003 Posts: 49 Location: London, UK
|
Posted: Mon Dec 22, 2003 3:17 pm Post subject: 'Previous' | 'Next' Pagination plugin |
|
|
Hi all,
I've seen this requested and / or asked about many times in many places in many formats and as I've written a few custom galleries for people, pagination is all too useful. My latest versions have been using Smarty, so it made sense to try and write a plugin.. so here's my first ever! =)
It works for me.. running Apache 1.3.27 / PHP 4.3.2 / Smarty 2.6.0 on FreeBSD but I haven't tested anywhere else as yet. It's also still a little roughly coded and I'd like to tidy it up a bit for a 0.0.2 release if anyone finds this of any use.
Drop it into your Smarty/libs/plugins dir as function.pagination.php and call as {pagination ...params here...}
I hope it's of use.. and all feedback, good or bad welcomed =)
[php:1:77b96694bc]<?php
/***************************************************************************
* Smarty {pagination} Plugin
*
* File...: function.pagination.php
* Type...: Function
* Name...: pagination
* Version: 0.0.1
* Date...: 2003-12-22
* Author.: Ian.H <ian@digiserv.net>
* Purpose: Provide "previous" | "next" page links
*
*
* Param Options:
* --------------
* str page_file | Used for /foo.php?pg=1 style URIs
* int page | Current page: ?pg=1
* int max_items | Max items to display per page
* int total_items | Total number of items for displaying
* [str prev_image] | Optional path to image for 'Previous'
* [str next_image] | Optional path to image for 'Next'
* | Both of the above paths are relative to
* | the DOCUMENT_ROOT
* [str google_friendly] | Default: no (yes|no) Use 'foo.php/1'
* | rather than 'foo.php?pg=1'
* | (your code needs to use the correct
* | format itself to use this option.
* | ie: $_SERVER['PATH_INFO'])
* [str use_query_string] | Default: yes (yes|no) Whether or not
* | to use the query '/1' or '?pg=1' style
* | URIs or not. This option overrides
* | page_file if defined as "no"
* [str prev_page] | If not using the query_string method, you
* | can specify 'page2.php' for example
* [str next_page] | Likewise for prev_page, the next page can
* | also be defined.
*
*
* Example:
* --------
* {pagination page_file="/gallery.php" page=$page_num max_items=6 total_items=$total_items prev_image="/graphics/prev.png" next_image="/graphics/next.png" google_friendly="yes"}
*
* {pagination page=$page_num max_items=6 total_items=$total_items use_query_string="no" prev_page="/page1.php" next_page="/page3.php"}
*
****************************************************************************/
function smarty_function_pagination($params, &$smarty) {
extract($params);
if (strlen($page_file) < 1) die('No page!');
if (intval($page) < 1) $page = 1;
$_query_string_type = (strtolower($google_friendly) == 'yes') ? '/' : '?pg=';
if (strlen($prev_image) > 0) {
$_previous = '<img src="' . $prev_image . '" alt="Prev" title="Previous Page" />';
} else {
$_previous = 'Prev';
}
if (strlen($next_image) > 0) {
$_next = '<img src="' . $next_image . '" alt="Next" title="Next Page" />';
} else {
$_next = 'Next';
}
if (strtolower($use_query_string) != 'no') $use_query_string = 'yes';
if (strtolower($use_query_string) == 'no') {
$_prev_page_file = $prev_page;
$_next_page_file = $next_page;
$_query_string_type = '';
} else {
$_prev_page_file = $_next_page_file = $page_file;
}
if (intval($page) > 1) {
if (strtolower($use_query_string) == 'no') {
$_prev_url = $prev_page;
} else {
$_prev_url = $page_file . $_query_string_type . (intval($page) - 1);
}
$_page_link = '<a href="' . $_prev_url . '">' . $_previous . '</a>';
}
if ((intval($page) * $max_items) < $total_items) {
$_page_link .= (intval($page) > 1) ? '&&&' : '';
if (strtolower($use_query_string) == 'no') {
$_next_url = $next_page;
} else {
$_next_url = $page_file . $_query_string_type . (intval($page) + 1);
}
$_page_link .= '<a href="' . $_next_url . '">' . $_next . '</a>';
} else {
if (!empty($_page_link)) {
$_page_link .= '';
}
}
return $_page_link;
}
?>
[/php:1:77b96694bc]
Regards,
tk _________________ Certified Code Junkie(tM)
Copyleft (L) two thousand and now, tk |
|
Back to top |
|
ZeroFill Smarty Rookie
Joined: 31 Aug 2003 Posts: 28
|
Posted: Tue Jan 27, 2004 11:16 am Post subject: |
|
|
Made a little change. Instead of &&& between Prev and Next, a jump box is there instead. I also removed the $page_file requirement (/?pg=1 works fine) and if the $page_file was already using "?" it'll use "&" instead (ex: /gallery.php?cat=5&pg=2).
Use newer version below _________________ my delimiters are <{ and }>
Last edited by ZeroFill on Sun Feb 01, 2004 11:03 am; edited 1 time in total |
|
Back to top |
|
ZeroFill Smarty Rookie
Joined: 31 Aug 2003 Posts: 28
|
Posted: Sun Feb 01, 2004 11:02 am Post subject: |
|
|
Made another itteration of this function. Here are the changes:
- Insert a class type via the class variable (CSS)
- Change the default $_GET variable via name
- Current page in dropdown now sticks (checked)
Both additions above are optional
[php:1:23f5f9c5ae]
<?php
/***************************************************************************
* Smarty {pagination} Plugin
*
* File...: function.pagination.php
* Type...: Function
* Name...: pagination
* Version: 0.0.1.02
* Date...: 2004-2-1
* Author.: Ian.H <ian@digiserv.net>
* Modified by: ZeroFill <smarty@zer0fill.com>
* Purpose: Provide "previous" | "next" page links
*
*
* Param Options:
* --------------
* str page_file | Used for /foo.php?pg=1 style URIs
* int page | Current page: ?pg=1
* int max_items | Max items to display per page
* int total_items | Total number of items for displaying
* [str prev_image] | Optional path to image for 'Previous'
* [str next_image] | Optional path to image for 'Next'
* | Both of the above paths are relative to
* | the DOCUMENT_ROOT
* [str google_friendly] | Default: no (yes|no) Use 'foo.php/1'
* | rather than 'foo.php?pg=1'
* | (your code needs to use the correct
* | format itself to use this option.
* | ie: $_SERVER['PATH_INFO'])
* [str use_query_string] | Default: yes (yes|no) Whether or not
* | to use the query '/1' or '?pg=1' style
* | URIs or not. This option overrides
* | page_file if defined as "no"
* [str prev_page] | If not using the query_string method, you
* | can specify 'page2.php' for example
* [str next_page] | Likewise for prev_page, the next page can
* | also be defined.
* [str name] | Change the default name (pg)
* [str class] | adds a class type to the <select> statement
*
* Example:
* --------
* {pagination page_file="/gallery.php" page=$page_num max_items=6 total_items=$total_items prev_image="/graphics/prev.png" next_image="/graphics/next.png" google_friendly="yes"}
*
* {pagination page=$page_num max_items=6 total_items=$total_items use_query_string="no" prev_page="/page1.php" next_page="/page3.php"}
*
****************************************************************************/
function smarty_function_pagination($params, &$smarty) {
extract($params);
echo $name,'<<<<<';
//if (strlen($page_file) < 1) die('No page!');
$name = (!empty($name)) ? $name : 'pg';
$joiner = (strstr($page_file,'?')) ? '&' : '?';
if (intval($page) < 1) $page = 1;
$_query_string_type = (strtolower($google_friendly) == 'yes') ? '/' : $joiner.$name.'=';
if (strlen($prev_image) > 0) {
$_previous = '<img src="' . $prev_image . '" alt="Prev" title="Previous Page" />';
} else {
$_previous = 'Prev';
}
if (strlen($next_image) > 0) {
$_next = '<img src="' . $next_image . '" alt="Next" title="Next Page" />';
} else {
$_next = 'Next';
}
if (strtolower($use_query_string) != 'no') $use_query_string = 'yes';
if (strtolower($use_query_string) == 'no') {
$_prev_page_file = $prev_page;
$_next_page_file = $next_page;
$_query_string_type = '';
} else {
$_prev_page_file = $_next_page_file = $page_file;
}
if (intval($page) > 1) {
if (strtolower($use_query_string) == 'no') {
$_prev_url = $prev_page;
} else {
$_prev_url = $page_file . $_query_string_type . (intval($page) - 1);
}
$_page_link = '<a href="' . $_prev_url . '">' . $_previous . '</a>';
}
if ((intval($page) * $max_items) < $total_items) {
if (intval($page) > 1)
{
$_page_link .= '&<form method="post" style="margin: 0px; display:inline">'."\n"
.'<select name="'.$name.'" onChange="location.href=this.form.'.$name.'.value" class="'.$class.'">';
for ($i=0;$i<intval($total_items);$i++)
{if ($_GET[$name] == $i) $selected = 'selected';
$_page_link .= '<option value='.$page_file.$_query_string_type.$i.' '.$selected.'>'.($i+1).'</option>';
$selected = null;
}
$_page_link .= '</select></form>&';
}
else
{
$_page_link .= '';
}
if (strtolower($use_query_string) == 'no') {
$_next_url = $next_page;
} else {
$_next_url = $page_file . $_query_string_type . (intval($page) + 1);
}
$_page_link .= '<a href="' . $_next_url . '">' . $_next . '</a>';
} else {
if (!empty($_page_link)) {
$_page_link .= '';
}
}
return $_page_link;
}
?>
[/php:1:23f5f9c5ae]
Example
Code: |
<{pagination page_file="" page=2 max_items=50 name="p" total_items=1000 google_friendly="no"}>
|
_________________ my delimiters are <{ and }> |
|
Back to top |
|
AmericanD Smarty n00b
Joined: 12 Jul 2004 Posts: 2
|
Posted: Mon Jul 12, 2004 5:49 pm Post subject: |
|
|
ZeroFill : I have a small correction for your code.
In Line 90 please change the 1 to 0 like this
if (intval($page) > 0)
So now if the page number is 1 then it will display the next form, earlier it was not showing anything, basically went to the else part .
Thanks though for the sweet plug in
Edit:-
Second correction
In line 96 , change the ($i+1) to $i please. It displays the next page instead which is incorrect.
Thanks again
Edit:-
Line 94 should be replaced with this
for ($i=1;$i<=intval($total_items);$i++) |
|
Back to top |
|
AmericanD Smarty n00b
Joined: 12 Jul 2004 Posts: 2
|
Posted: Mon Jul 12, 2004 7:22 pm Post subject: |
|
|
Ignore my previous reply.
I had to change a whole bunch of things to make it working properly. From line 40 onwards
[php:1:62b1f85b6a]<?php
if ((intval($page) * $max_items) <= ($total_items + $max_items)) {
if (intval($page) > 0)
{
$_page_link .= '&<form method="post" style="margin: 0px; display:inline">'."\n"
.'<select name="'.$name.'" onChange="location.href=this.form.'.$name.'.value" class="'.$class.'">';
for ($i=1;$i<=ceil(intval($total_items) / intval($max_items));$i++)
{if ($_GET[$name] == $i){ $selected = 'selected';
$_page_link .= '<option value='.$page_file.$_query_string_type.$i.' '.$selected.'>'.$i.'</option>';
}
else {
$selected = null;
$_page_link .= '<option value='.$page_file.$_query_string_type.$i.' '.$selected.'>'.$i.'</option>';
}
?>[/php:1:62b1f85b6a]
?>[/php] |
|
Back to top |
|
arleen Smarty n00b
Joined: 22 May 2004 Posts: 1 Location: Laguna Beach / San Diego CA
|
Posted: Thu Aug 12, 2004 1:48 am Post subject: Re: Previous | Next Pagination plugin |
|
|
Thanks to the three of you for the plug-in and mods. Can someone please provide a full example of the usage? PLEASE??? Just wrote an MLS search engine and am on deadline. Thank you in advance.
Arleen. |
|
Back to top |
|
limbhg Smarty Regular
Joined: 18 Jun 2004 Posts: 62
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Sep 29, 2004 3:48 am Post subject: |
|
|
Hi limbhg,
No it is not, but fortunately it continues to be available in this thread
In fact, I'm not aware of any plans to change the mix of plugins contained in the main distribution for any upcoming release.
Greetings, |
|
Back to top |
|
limbhg Smarty Regular
Joined: 18 Jun 2004 Posts: 62
|
Posted: Wed Sep 29, 2004 6:23 am Post subject: |
|
|
Hi,
It is not that good meh? I believe it is a good one, why don't just include it into the distribution?
I need to have the Prev/Next feature.
Regards.
boots wrote: | Hi limbhg,
No it is not, but fortunately it continues to be available in this thread
In fact, I'm not aware of any plans to change the mix of plugins contained in the main distribution for any upcoming release.
Greetings, |
|
|
Back to top |
|
tk Smarty Regular
Joined: 31 Oct 2003 Posts: 49 Location: London, UK
|
Posted: Tue Nov 16, 2004 2:47 pm Post subject: |
|
|
Not checked this for a long time.. but just FWIW boots.. _if_ you ever decide to include more plugins in the core distribution and fancy adding this.. I can't speak for the mods to it I didn't make.. but the version at least that I wrote you're more than welcome to include (no need to ask).
Maybe I should write a small example script and package it up as a tarball and post a link to a proper download to acompany the thread. Can someone add an extra couple of hours to the day.... please? =)
Regards,
tk _________________ Certified Code Junkie(tM)
Copyleft (L) two thousand and now, tk |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Nov 17, 2004 1:56 am Post subject: |
|
|
Hi tk! Always a pleasure to see you here.
I think the best places to post plugins are here and also at the wiki. As for adding new core plugins, I just want to note that I only provide what I know of the current intentions based on messages in the forum, dev list and from emails. The final arbitor for such issues is Monte (or messju) whereas I only give opinion
For a long while there has been talk of a new forum that would include a user moderated library for plugins. When someone gets off their hands for long enough (or someone takes your suggestion and extends the number of hours in the day) it just may happen.
Your generosity is well noted--please continue to share what you will as it evidently useful to other users. As for this particular plugin--you haven't been around here in a bit but shortly after my response to limbhg, Monte went and released SmartyPaginate (available in the Addons forum). All these choices make the world go round, huh?
Cheers! |
|
Back to top |
|
gregolin Smarty Rookie
Joined: 17 Jun 2004 Posts: 14
|
Posted: Wed Nov 17, 2004 9:55 pm Post subject: And the query? |
|
|
Hi.
The HTML of pagination is ok... but I need to put "LIMIT X, Y" in the query... how can I do this?
Thanks. _________________ []'s Leandro |
|
Back to top |
|
teknon Smarty Rookie
Joined: 13 May 2006 Posts: 6
|
Posted: Sat May 13, 2006 6:07 am Post subject: Pagination Function Plugin |
|
|
I want to use this plugin. However, I need two questions answered. Does it work? How do I plug it in?
I have a template which creates a list from a smarty section loop command. I want to break the list into a display of 20 each page. Do I put the code in the template or the file with the database search. Also, what is the syntax. Do I have to include the plugin at the top of the file. Please help, I am brand new to smarty. |
|
Back to top |
|
teknon Smarty Rookie
Joined: 13 May 2006 Posts: 6
|
Posted: Sat May 27, 2006 5:18 am Post subject: I still need help |
|
|
Hello. I have used this plugin. I am using the first one, because the changes where not clear in regards to where to place them. My numbering seems to be different. However, I am getting the link, but the list is not being limited. Do I have to limit it in the query? Please help. |
|
Back to top |
|
|