View previous topic :: View next topic |
Author |
Message |
chengfu Smarty n00b
Joined: 13 Apr 2004 Posts: 2
|
Posted: Tue Apr 13, 2004 7:30 pm Post subject: New function: Sliding pager |
|
|
I just finished a new pager plugin. I got the idea (but no code) from the PEAR package PEAR::Pager.
If too many pages are available it will show only a limited set but still allow easy navigation to the nearby pages, first page and last page. If e.g. 17 pages exist, only 5 may be shown and the currently active page is number 7 the output will look like this:
<< < 1 ... 5 6 7 8 9 ... 17 > >>
Most of the behaviour is configurable. Please let me know if any features are missing or if you encounter any errors. I've been trying it out on http://www.dialer-webmaster.de/ and couldn't find any errors until now.
Code: |
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: sliding_page
* Purpose: create a sliding-pager for page browsing
* Version: 0.1
* Date: April 11, 2004
* Last Modified: April 11, 2004
* Author: Mario Witte <mario dot witte at chengfu dot net>
* HTTP: http://www.chengfu.net/
* -------------------------------------------------------------
*/
function smarty_function_sliding_pager($params, &$smarty)
{
/*
@param mixed $pagecount - number of pages to browse
@param int $linknum - max. number of links to show on one page (default: 5)
@param int $curpage - current page number
@param string baseurl - baseurl to which the pagenumber will appended
@param string url_append - text to append to url after pagenumber, e.g. "html" (default: "")
@param string txt_first - text for link to first page (default: "&&")
@param string txt_prev - text for link to previous page (default: "&")
@param string separator - text to print between page numbers (default: "&|&")
@param string txt_next - text for link to next page (default: "&")
@param string txt_last - text for link to last page (default: "&&")
@param string txt_skip - text shown when page s are skipped (not shown) (default: "&...&")
@param string css_class - css class for the pager (default: "")
@param boolean link_current - whether to link the current page (default: false)
*/
/* Define all vars with default value */
$linknum = 5;
$url_append= '';
$txt_first = '&&';
$txt_prev = '&';
$separator = '&|&';
$txt_next = '&';
$txt_last = '&&';
$txt_skip = '&...&';
$css_class = '';
$link_current = false;
/* Import parameters */
extract($params);
/* Convert page count if array */
if (is_array($pagecount)) $pagecount = sizeof($pagecount);
/* Define additional required vars */
$delta_l = 0;
$delta_r = 0;
if ($linknum % 2 == 0) {
$delta_l = ($linknum / 2 ) - 1;
$delta_r = $linknum / 2;
} else {
$delta_l = $delta_r = ($linknum - 1) / 2;
}
/* Check parameters */
if (empty($pagecount)) {
$smarty->trigger_error("sliding_pager: missing 'pagecount' parameter");
return;
}
if (empty($curpage)) {
$smarty->trigger_error("sliding_pager: missing 'curpage' parameter");
return;
}
if (empty($baseurl)) {
$smarty->trigger_error("sliding_pager: missing 'baseurl' parameter");
return;
}
/* There is no 0th page */
$curpage = $curpage == 0 ? 1 : $curpage;
/* Internally we need an "array-compatible" index */
$int_curpage = $curpage - 1;
/* Paging needed? */
if ($pagecount <= 1) {
// No paging needed for one page
return;
}
/* Build all page links (we'll delete some later if required) */
$links = array();
for($i = 0; $i < $pagecount; $i++) {
$links[$i] = $i + 1;
}
/* Sliding needed? */
if ($pagecount > $linknum) { // Yes
if (($int_curpage - $delta_l) < 1) { // Delta_l needs adjustment, we are too far left
$delta_l = $int_curpage - 1;
$delta_r = $linknum - $delta_l - 1;
}
if (($int_curpage + $delta_r) > $pagecount) { // Delta_r needs adjustment, we are too far right
$delta_r = $pagecount - $int_curpage;
$delta_l = $linknum - $delta_r - 1;
}
if ($int_curpage - $delta_l > 1) { // Let's do some cutting on the left side
array_splice($links, 0, $int_curpage - $delta_l);
}
if ($int_curpage + $delta_r < $pagecount) { // The right side will also need some treatment
array_splice($links, $int_curpage + $delta_r + 2 - $links[0]);
}
}
/* Build link bar */
$retval = '';
$css_class = $css_class ? 'class="'.$css_class.'"' : '';
if ($curpage > 1) {
$retval .= '<a href="'.$baseurl.'1'.$url_append.'" '.$css_class.'>'.$txt_first.'</a>';
$retval .= $separator;
$retval .= '<a href="'.$baseurl.($curpage - 1).$url_append.'" '.$css_class.'>'.$txt_prev.'</a>';
$retval .= $separator;
}
if ($links[0] != 1) {
$retval .= '<a href="'.$baseurl.'1'.$url_append.'" '.$css_class.'>1</a>';
if ($links[0] == 2) {
$retval .= $separator;
} else {
$retval .= $txt_skip;
}
}
for($i = 0; $i < sizeof($links); $i++) {
if ($links[$i] != $curpage or $link_current) {
$retval .= '<a href="'.$baseurl.$links[$i].$url_append.'" '.$css_class.'>'.$links[$i].'</a>';
} else {
$retval .= $links[$i];
}
if ($i < sizeof($links) - 1) {
$retval .= $separator;
}
}
if ($links[sizeof($links) - 1] != $pagecount) {
if ($links[sizeof($links) - 2] != $pagecount - 1) {
$retval .= $txt_skip;
} else {
$retval .= $separator;
}
$retval .= '<a href="'.$baseurl.$pagecount.$url_append.'" '.$css_class.'>'.$pagecount.'</a>';
}
if ($curpage != $pagecount) {
$retval .= $separator;
$retval .= '<a href="'.$baseurl.($curpage + 1).$url_append.'" '.$css_class.'>'.$txt_next.'</a>';
$retval .= $separator;
$retval .= '<a href="'.$baseurl.$pagecount.$url_append.'" '.$css_class.'>'.$txt_last.'</a>';
}
return $retval;
}
/* vim: set expandtab: */
/* vim: set ts=4: */
?>
|
Thanks for all your comments, I don't want to publish it on the smarty wiki before I'm sure that everything is working.
Bye, CF _________________ http://www.chengfu.net/
Last edited by chengfu on Wed Aug 04, 2004 4:44 pm; edited 1 time in total |
|
Back to top |
|
Adar Smarty Regular
Joined: 27 May 2004 Posts: 58
|
Posted: Wed Jun 09, 2004 1:44 pm Post subject: |
|
|
how to use it? |
|
Back to top |
|
the-styler Smarty Rookie
Joined: 12 Jul 2004 Posts: 25
|
Posted: Mon Jul 12, 2004 6:41 pm Post subject: |
|
|
that looks good, but how can i use it? |
|
Back to top |
|
Kevin Smarty Rookie
Joined: 06 Jan 2004 Posts: 19
|
Posted: Sun Aug 01, 2004 6:39 pm Post subject: |
|
|
its not working yet
________
Mini E
Last edited by Kevin on Fri Feb 18, 2011 11:41 am; edited 1 time in total |
|
Back to top |
|
chengfu Smarty n00b
Joined: 13 Apr 2004 Posts: 2
|
Posted: Wed Aug 04, 2004 4:49 pm Post subject: |
|
|
Hello,
I'm really sorry for answering so late, but I don't check this forum very often.
Your app needs to calculate the number of pages it has to display, what page you are on etc. Sliding_Pager only does the display.
A very simple call to sliding_pager might look like this:
{sliding_pager baseurl="/page_" url_append=".html" pagecount=$total_pagenumber curpage=$cur_pagenumber}
It will set the maximum number of pages to $total_pagenumber, the current page number is set to $cur_pagenumber and the resulting links will look like e.g. <a href="/page_5.html">5</a>
I hope this helps a little...
@Kevin:
What exactly isn't working? I've made a very small change to the code above (a comment wasn't closed), but apart from that this is exactly the same function that I'm currently using in at least 4 websites.
Bye, CF _________________ http://www.chengfu.net/ |
|
Back to top |
|
timurv Smarty Rookie
Joined: 07 Sep 2004 Posts: 8 Location: Russia, Kazan
|
Posted: Wed Sep 22, 2004 11:23 am Post subject: sprintf |
|
|
May be insted off
Code: | baseurl="/page_" url_append=".html" |
more usefull:
Code: | baseurl_fmt="/page_%d.html" |
And then in plugin code:
simple sprintf($baseurl_fmt, $pagecount) |
|
Back to top |
|
the-styler Smarty Rookie
Joined: 12 Jul 2004 Posts: 25
|
Posted: Thu Sep 23, 2004 3:09 pm Post subject: |
|
|
In which line I must change the code who timurv has posted |
|
Back to top |
|
toolpixx Smarty n00b
Joined: 04 May 2006 Posts: 1
|
Posted: Thu May 04, 2006 10:45 pm Post subject: |
|
|
works great and that is what a wan't for output...
thanks for this function.
regards _________________ please give a little time to be ready... |
|
Back to top |
|
|