PHP Classes

File: SPagin.php

Recommend this page to a friend!
  Classes of charles schaefer   SPagin   SPagin.php   Download  
File: SPagin.php
Role: Class source
Content type: text/plain
Description: the class file
Class: SPagin
Generate navigation links for pagination
Author: By
Last change: Alguns bugs corrigidos, principalmente no método SPagin::paginar_inteligente().

English -------------

Some bugs was corrected, especialy that of the SPagin::paginar_inteligente() method.
Date: 18 years ago
Size: 11,634 bytes
 

Contents

Class file image Download
<?php /** SPagin is a free php class that generate a simple pagination and return a string with the links already formated. Copyright (C) 2006 Charles Schaefer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** * Classe que gera a pagina&#65533;&#65533;o com limite de exibi&#65533;&#65533;o de resutlados. * @copyright Charles Schaefer - 2006 * */ class SPagin{ var $regs,$exNum,$url,$totReg,$totPag,$pg; var $separador,$tag_pagin; /** * Separador da url. Pode ser ? ou & * * @var string */ var $url_separator; /** * @desc M&#65533;todo construtor da classe * * @param string $regs numero de registro por p&#65533;gina * @param string $url endere&#65533;o da p&#65533;gina onde cont&#65533;m a pagina&#65533;&#65533;o * @param integer $totReg total de registros contidos no Banco de Dados * @param integer $pg em qual p&#65533;ina estamos * @param integer $exNum nmero de p&#65533;ginas a ser exibida na pagina&#65533;&#65533;o ex.: exNum =5 (<< < 1 2 3 4 5 > >>) * @param string $separador padr&#65533;o de separa&#65533;&#65533;o dos links * @param string $tag_pagin tag na qual ser&#65533; inserida a pagina&#65533;&#65533;o * @return none */ function SPagin($regs,$url,$totReg,$pg,$exNum="",$separador=' | ',$tag_pagin=""){ $this->regs = $regs; $this->exNum = $exNum;//essa vari&#65533;vel deve ser usada para criar uma forma de exibir apenas a quantidade de numeros pedida $this->url = $url; $this->totReg = $totReg; $totPag = $totReg<=$regs ? 1 : ceil($totReg/$regs); $this->totPag = $totPag; $this->pg = ($pg != "") ? $pg : 1; $this->separador = $separador != "" ? $separador : " | "; $this->tag_pagin = $tag_pagin; $this->url_separator = (strpos($url,"?") !== false) ? "&" : "?"; } /** * @desc m&#65533;todo que ir&#65533; definir os argumentos para serem repassados no "limit", quando se estiver utilizando um banco de dados * @return string limit formatado para usar como cl&#65533;usula no sql */ function limit(){ $inicio = ($this->pg * $this->regs) - $this->regs;//o primeiro registro a ser puxado do BD $final = $this->regs;//o &#65533;ltimo a ser puxado do BD $limit = $inicio.",".$final; return $limit; } /** * @desc p&#65533;gina que vai devolver o resultado da pagina&#65533;&#65533;o * * @param string $primeiro o que usar na pagina&#65533;&#65533;o com inicador de primeiro * @param string $ultimo o que usar na pagina&#65533;&#65533;o com inicador de ultimo * @param string $prev o que usar na pagina&#65533;&#65533;o com inicador de pr&#65533;ximo * @param string $next o que usar na pagina&#65533;&#65533;o com inicador de anterior * @return string menu formatado, pronto para ser exibido na tela */ function paginar($primeiro="<<",$ultimo=">>",$prev="<",$next=">"){ $pg = $this->pg; $totPag = $this->totPag; $url = $this->url; $tag_pagin = $this->tag_pagin; $url_separator = $this->url_separator; $primeiro = $primeiro == "" ? "<<" : $primeiro; $ultimo = $ultimo == "" ? ">>" : $ultimo; $prev = $prev == "" ? "<" : $prev; $next = $next == "" ? ">" : $next; if($totPag == $pg) { $pg = $this->pg - 1; $proximo = $next." "; $ultima = $ultimo; $anterior = "<a href=\"".$url.$url_separator."pg=$pg\"> ".$prev."</a> "; $primeira = "<a href=\"".$url.$url_separator."pg=1\">".$primeiro."</a>"; }elseif($pg == 1) { $pg = $this->pg + 1; $proximo = "<a href=\"".$url.$url_separator."pg=$pg\">".$next." </a>"; $anterior = " ".$prev." "; $primeira = $primeiro; $ultima = "<a href=\"".$url.$url_separator."pg=$totPag\">".$ultimo."</a>"; }else { $pg_p = $pg + 1; $pg_a = $pg - 1; $proximo = "<a href=\"".$url.$url_separator."pg=$pg_p\">".$next." </a>"; $anterior = "<a href=\"".$url.$url_separator."pg=$pg_a\"> ".$prev."</a> "; $primeira = "<a href=\"".$url.$url_separator."pg=1\">".$primeiro."<a/>"; $ultima = "<a href=\"".$url.$url_separator."pg=$totPag\">".$ultimo."</a>"; } $pagina = "<p>".$primeira.$anterior; if($this->exNum != "") { $exNum = $this->exNum; $pagina .= $this->paginar_limitado($exNum,$totPag); }else { for($i=1;$i<=$totPag;$i++) { if($i == $this->pg) { $pagina .= "<b>".$i."</b> | "; } else { $pagina .= "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a> | "; } }//close for } $pagina .= $proximo.$ultima."</p>"; if($totPag == 1) { $result = "<span style=\"visibility: hidden\">".$pagina."</span>"; }elseif ($tag_pagin != "") { $result = str_replace("><",">".$pagina."<",$tag_pagin); }else { $result = $pagina; } return $result; }//close function /** * @desc Fun&#65533;&#65533;o que ir&#65533; criar um menu de navega&#65533;&#65533;o de p&#65533;ginas com limite de itens (no menu) ex.: << < 1 2 3 4 5 > >> * * @param integer $exNum * @param integer $totalPg * @return string menu formatado */ function paginar_limitado($exNum,$totalPg) { $url = $this->url; $url_separator = $this->url_separator; $separador = $this->separador; $pg = $this->pg; $div = (is_int($exNum/2)) ? $exNum/2 : floor($exNum/2); $centro = (is_int($exNum/2)) ? $exNum/2 : ceil($exNum/2); if($pg == $totalPg) { if($totalPg >= $exNum) { $termina = $totalPg; $comeco = $termina - $exNum +1; $i =$comeco; while($i>=$comeco and $i<=$termina) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } }else { $comeco = 1; $termina = $totalPg; $i = $comeco; while($i>=$comeco and $i<=$termina) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } } }elseif ($pg == 1) { $comeco = 1; $termina = $totalPg >= $exNum ? ($comeco + $exNum) -1 : $totalPg; $i = $comeco; //echo $i." ".$termina." ".$exNum." ".$this->exNum; while($i <= $termina and $i >= $comeco) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } }elseif ($pg < $totalPg - $exNum) { if($pg > $exNum) { $comeco = $pg - $div; $termina = $comeco + $exNum - 1; $i = $comeco; while ($i <= $termina and $i >= $comeco) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } }elseif ($pg < $exNum) { if($pg > $centro) { $comeco = $pg - $div; $termina = $comeco + $exNum - 1; $i = $comeco; while($i <= $termina and $i >= $comeco) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } }else { $comeco = 1; $termina = $comeco + $exNum - 1; $i = $comeco; while($i <= $termina and $i >= $comeco) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } } }elseif ($pg == $exNum) { $comeco = $exNum - $div; $termina = $comeco + $exNum -1; $i = $comeco; while ($i >= $comeco and $i <= $termina) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } } }elseif ($pg > $totalPg - $exNum){ if($totalPg > $exNum) { //$comeco = $totalPg - $exNum; //$termina = $comeco + $exNum - 1; if($pg <= $totalPg - ($exNum - $centro)) { $comeco = $pg - $div; $termina = $comeco + $exNum - 1; $i = $comeco; while ($i >= $comeco and $i <= $termina) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } }else { $comeco = $totalPg - $exNum + 1; $termina = $comeco + $exNum - 1; $i = $comeco; while ($i >= $comeco and $i <= $termina) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } } }elseif ($totalPg <= $exNum) { $comeco = 1; $termina = $totalPg; $i = $comeco; while ($i <= $termina and $i >= $comeco) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } } }elseif ($pg == $totalPg - $exNum) { $div = floor($exNum / 2); $termina = $totalPg - ($exNum - $div); $comeco = $termina - $exNum + 1; $i = $comeco; while ($i <= $termina and $i >= $comeco) { $menu .= ($i == $pg) ? "<b>".$i."</b>$separador" : "<a href=\"".$url.$url_separator."pg=".$i."\">".$i."</a>$separador"; $i++; } } //die($menu); $menu = rtrim($menu,$separador)." "; return $menu; }//close function /** * @desc fun&#65533;&#65533;o que gera a pagina&#65533;&#65533;o inteligente, no formato item y a x de n * * @param string $template deve estar no formato "blabla #1 blabla #2 blabla #3 blabalbalbal" sendo que #1,#2,#3 s&#65533;o inicio, final, total, respectivamente. Ex.: item #1 a #2 de #3 ficara "item 10 a 20 de 45" * @return string menu formatado */ function paginar_inteligente($template="") { $menu = ""; $de = $this->pg * $this->regs - $this->regs + 1; $a = $this->pg * $this->regs; $total = $this->totReg; ($total - $a) < $this->exNum ? $a = $total : ""; if(!empty($template)) { $tpl = substr_replace(substr_replace(substr_replace($template,$total,strpos($template,"#3"),2),$a,strpos($template,"#2"),2),$de,strpos($template,"#1"),2); $menu = $tpl; }else { $menu = "Itens ".$de." a ".$a." de ".$total; } return $menu; } } ?>