trait Pagination{
private $totalRegistros;
private $limite;
private $offset;
private $totalPaginas;
private $segmentoUrl;
private $links;
private $explodeBarrasUrl;
private $posicaoPaginaUrl;
static $nextLabel = '>>';
static $prevLabel = '<<';
public function setTotalRegistros($totalRegistros){
$this->totalRegistros = $totalRegistros;
}
public function setLimite($limite){
//Limita o número de dados do banco a serem mostrados por página
$this->limite = $limite;
}
private function encontrarPagNaUrl(){
$this->explodeBarrasUrl = explode('/', $_SERVER['REQUEST_URI']);
$this->posicaoPaginaUrl = array_search('pag', $this->explodeBarrasUrl);
}
private function paginaAtual(){
$this->encontrarPagNaUrl();
return (!$this->posicaoPaginaUrl) ? 1 : $this->explodeBarrasUrl[$this->posicaoPaginaUrl+1];
}
private function offset(){
return $this->offset = ($this->paginaAtual() * $this->limite) - $this->limite;
}
private function totalPaginas(){
return $this->totalPaginas = ceil($this->totalRegistros / $this->limite);
}
private function nextLink(){
if ($this->paginaAtual() < (int)$this->totalPaginas()){
return '<a href="http://' . $_SERVER['HTTP_HOST'] . '/' . $this->segmentoUrl . 'pag/' . ($this->paginaAtual()+1) . '">' . self::$nextLabel . '</a>';
}
}
private function prevLink(){//vou sair ja voltook
if ($this->paginaAtual() <= (int)$this->totalPaginas() && $this->paginaAtual() > 1){
return '<a href="http://' . $_SERVER['HTTP_HOST']. '/' . $this->segmentoUrl . 'pag/' . ($this->paginaAtual()-1) . '">' . self::$prevLabel . '</a>';
}
}
private function temPagUrl(){
for ($i=0; $i < $this->posicaoPaginaUrl; $i++) {
$this->segmentoUrl .= $this->explodeBarrasUrl[$i].'/';
}
$this->segmentoUrl = ltrim($this->segmentoUrl, '/');
}
private function naoTemPagUrl(){
$this->segmentoUrl = $_SERVER['REQUEST_URI'].'/';
}
private function segmentoUrl(){
$numeroBarrasUrl = substr_count($_SERVER['REQUEST_URI'], '/');
if ($numeroBarrasUrl > 1) {
$this->encontrarPagNaUrl();
(!$this->posicaoPaginaUrl) ? $this->naoTemPagUrl() : $this->temPagUrl();
} else {
$this->segmentoUrl = $_SERVER['REQUEST_URI'].'/';
$this->segmentoUrl = ltrim($this->segmentoUrl);
}
}
public function render(){
$this->segmentoUrl();
$this->links .= '<div>';
$this->links .= '<ul class="pagination">';
$this->links .= '<li>' . $this->prevLink() . '</li>';
for ($i=1; $i <= $this->totalPaginas(); $i++){
$this->links .= '<li' . ($i == $this->paginaAtual() ? ' class="active"' : '') . '><a href="http://'.$_SERVER['HTTP_HOST'].$this->segmentoUrl.'pag/'.$i.'">'. $i . '</a></li>';
}
$this->links .= '<li>' . $this->nextLink() . '</li>';
$this->links .= '</ul>';
$this->links .= '</div>';
return $this->links;
}
public function paginate($sql, $paginate = null) {
return (is_null($paginate)) ? $sql : $sql . ' LIMIT ' . $this->limite . ' OFFSET ' . $this->offset();
}
}
I created this Trait
to paginate a project, but the render()
function is returning the following link on the buttons
link
For some reason it is adding an extra slash after the host, then when I click the button it does not find the page.