Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

New function: Sliding pager

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
chengfu
Smarty n00b


Joined: 13 Apr 2004
Posts: 2

PostPosted: Tue Apr 13, 2004 7:30 pm    Post subject: New function: Sliding pager Reply with quote

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
View user's profile Send private message Visit poster's website
Adar
Smarty Regular


Joined: 27 May 2004
Posts: 58

PostPosted: Wed Jun 09, 2004 1:44 pm    Post subject: Reply with quote

how to use it?
Back to top
View user's profile Send private message Visit poster's website
the-styler
Smarty Rookie


Joined: 12 Jul 2004
Posts: 25

PostPosted: Mon Jul 12, 2004 6:41 pm    Post subject: Reply with quote

that looks good, but how can i use it?
Back to top
View user's profile Send private message
Kevin
Smarty Rookie


Joined: 06 Jan 2004
Posts: 19

PostPosted: Sun Aug 01, 2004 6:39 pm    Post subject: Reply with quote

its not working yet Rolling Eyes
________
Mini E


Last edited by Kevin on Fri Feb 18, 2011 11:41 am; edited 1 time in total
Back to top
View user's profile Send private message
chengfu
Smarty n00b


Joined: 13 Apr 2004
Posts: 2

PostPosted: Wed Aug 04, 2004 4:49 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
timurv
Smarty Rookie


Joined: 07 Sep 2004
Posts: 8
Location: Russia, Kazan

PostPosted: Wed Sep 22, 2004 11:23 am    Post subject: sprintf Reply with quote

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
View user's profile Send private message
the-styler
Smarty Rookie


Joined: 12 Jul 2004
Posts: 25

PostPosted: Thu Sep 23, 2004 3:09 pm    Post subject: Reply with quote

In which line I must change the code who timurv has posted
Back to top
View user's profile Send private message
toolpixx
Smarty n00b


Joined: 04 May 2006
Posts: 1

PostPosted: Thu May 04, 2006 10:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP