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

Pagination made easy...

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
TommyD
Smarty Rookie


Joined: 20 Feb 2004
Posts: 18

PostPosted: Tue Aug 31, 2004 1:15 pm    Post subject: Pagination made easy... Reply with quote

Pagelinks for pagination support with support for i18n via config files.

Code:
<?php
/**
 *  The following code is released under LGPL available at
 *  http://www.gnu.org/copyleft/lesser.html.
 *
 *  You may not modify or remove this header.
 *
 *  Author: Thomas Keller <me@thomaskeller.biz>
 *
 *  (c) 2000-2004 MusicMadeMe.com - All rights reserved.
 */


/**
 * Smarty {pagelinks} function plugin
 *
 * Type:     function<br>
 * Name:     pagelinks<br>
 * Input:<br>
 *           - request    (required) - string
 *           - offset     (required) - number
 *           - total      (required) - number
 *           - rpp        (optional) - number default 20 (result rows per page)
 *           - lpp        (optional) - number default 12 (pagelinks per page)
 *           - back       (optional) - string default ""
 *           - forw       (optional) - string default ""
 *           - to_first   (optional) - string default ""
 *           - to_last    (optional) - string default ""
 *           - separator  (optional) - string default "..."
 * Purpose:  Prints a list of HTML pagelinks generated from
 *           the passed parameters
 * @param array
 * @param Smarty
 * @return string
 */
function smarty_function_pagelinks($params, &$smarty)
{
    $rpp        = isset($params['rpp']) ? $params['rpp'] : 20;
    $lpp        = isset($params['lpp']) ? $params['lpp'] : 20;
    $offset     = $params['offset'];
    $total      = $params['total'];
    $request    = $params['request'];

    if (isset($params['assign']) && empty($params['assign'])) {
        $compiler->_syntax_error("pagelinks: 'assign' parameter may not be empty.", E_USER_WARNING);
        return;
    }

    $write_output = !isset($params['assign']);

    //localized titles
    $back       = isset($params['back']) ? " title=\"".$params['back']."\" " : "";
    $forw       = isset($params['forw']) ? " title=\"".$params['forw']."\" " : "";
    $to_first   = isset($params['to_first']) ? " title=\"".$params['to_first']."\" " : "";
    $to_last    = isset($params['to_last']) ? " title=\"".$params['to_last']."\" " : "";

    //separator between pagelinks
    $separator  = isset($params['separator']) ? $params['separator'] : "...";

   //fits on one site, do nothing!
   if ($total <= $rpp)
    {
      return "";
   }

    //check if the request already contains some GET parameters
    //if not, append a "?" otherwise a "&"
    $request .= (strpos($request,"?")===false) ? "?" : "&";

    $curpage      = floor($offset/$rpp);

   //this is needed to set the right number of $totalpages for plane numbers
   if ($total % $rpp == 0)
    {
      $totalpages      = floor($total/$rpp)-1;
   }
    else
    {
      $totalpages      = ceil($total/$rpp)-1;
   }

   $maxleftright   = ceil($lpp/2);
   //only display first and last item if left/right pagelinks are cutted
   $leftdots       = false;
   $rightdots      = false;

   //determine how many pagelinks must be shown on the left and right
   //side of the current page
   if ($curpage<=$maxleftright)
    {
        //only right padding
      $leftpages   = $curpage;
      if ($lpp>$totalpages)
        {
         $rightpages   =  $totalpages-$leftpages;
      }
        else
        {
         $rightpages   = $lpp-$leftpages;
         $rightdots    = true;
      }
   }
    else if (($curpage+$maxleftright)>=$totalpages)
    {
        //only left padding
      $rightpages   = $totalpages-$curpage;
      if ($lpp>$totalpages)
        {
         $leftpages   = $totalpages-$rightpages;
      }
        else
        {
         $leftpages   = $lpp-$rightpages;
         $leftdots   = true;
      }
   }
    else
    {
        //for padding on both sides
      $leftpages   = $maxleftright;
      $rightpages   = $maxleftright;
      $leftdots = $rightdots = true;
   }

    $html = "";

   if ($curpage!=0)
    {
      $html .= "<a href=\"".$request."offset=".($offset-$rpp)."\" ".$back.
                 "&&&</a>&".$seperator;
   }

   if ($leftdots)
    {
      $html .= "<a href=\"".$request."offset=0\" ".$to_first.
                 "&&&</a>".$separator;
   }

   //set the page number of the first page link (zero based)
   $pageno = $curpage - $leftpages;

   //show navig for left ($i is not used)
   for($i=0;$i<$leftpages;$i++)
    {
      $html .= "<a href=\"".$request."offset=".($pageno*$rpp)."\">&".
                 ($pageno+1)."&</a>".$seperator."\n";
      $pageno++;
   }

   //display current page number
   $html .= "&<b>".($pageno+1)."</b>&\n";
   $pageno++;

   //show navig for right ($i is not used)
   for($i=0;$i<$rightpages;$i++)
    {
      $html .= "<a href=\"".$request."offset=".($pageno*$rpp)."\">&".
                 ($pageno+1)."&</a>".$seperator."\n";
      $pageno++;
   }

   if ($rightdots)
    {
      $html .= $seperator."&<a href=\"".$request."offset=".($totalpages*$rpp)."\" ".
                 $to_last.">&&&</a>";
   }

   if ($curpage!=$totalpages)
    {
      $html .= $seperator."&<a href=\"".$request."offset=".($offset+$rpp)."\" ".
                 $forw.">&&&</a>";
   }

    if ($write_output) return $html."\n";

    return $smarty->assign($params['assign'], $html."\n");
}

?>


Hope this helps someone,
Tommy.
_________________
Administrator / Developer on http://www.musicmademe.com
- http://thomaskeller.biz
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 -> Tips and Tricks 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