Created
March 1, 2012 17:06
-
-
Save davidchc/1951448 to your computer and use it in GitHub Desktop.
Exemplo de uma classe de Paginação
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Classe para Paginação de Resultado | |
* @author David | |
* | |
*/ | |
class Paginacao{ | |
private $pagina; | |
private $total; | |
private $quantidade; | |
private $url; | |
private $parametros; | |
public function __construct($pagina, $total_registros, $quantidade){ | |
$this->pagina = $pagina; | |
$this->total = $total_registros; | |
$this->quantidade = $quantidade; | |
} | |
public function setUrl($url){ | |
$this->url = $url; | |
} | |
public function setParametros($param){ | |
$this->parametros = $param; | |
} | |
public function getTotalPorPagina(){ | |
return ceil($this->total/$this->quantidade); | |
} | |
function gerarPaginacao($echo = true){ | |
$param = (is_array($this->parametros) && count($this->parametros) > 0) ? '&'.http_build_query($this->parametros) : ''; | |
$result = '<ul class="paginacao">'; | |
for($i = 1; $i <= $this->getTotalPorPagina(); $i++){ | |
if($this->pagina == $i){ | |
$result .= sprintf('<li><a href="#" class="pagiAtiva">%s</a></li>', $i); | |
}else{ | |
$result .= sprintf('<li><a href="%s?pag=%s%s">%s</a></li>', $this->url, $i, $param, $i); | |
} | |
} | |
$result .= '</ul>'; | |
if(!$echo){ | |
return $result; | |
} | |
echo $result; | |
} | |
} | |
/** | |
* Exemplo de utilização: | |
*/ | |
//Verifica se existe pag | |
$pagina = (isset($_GET['pag'])) ? (int)$_GET['pag'] : 1; | |
//Instancia a paginacao | |
$paginacao = new Paginacao($pagina, 20, 5); | |
//informa a url ou pagina que terá a paginacao | |
$paginacao->setUrl('categoria.php'); | |
//adiciona os parametros, com array | |
$paginacao->setParametros(array('categoria' => 5)); | |
//mostra os links da paginacao | |
$paginacao->gerarPaginacao(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muito obrigado por compartilhar sua classe, me ajudou muito.