Skip to content

Instantly share code, notes, and snippets.

@danjesus
Created September 16, 2011 19:04
Show Gist options
  • Select an option

  • Save danjesus/1222845 to your computer and use it in GitHub Desktop.

Select an option

Save danjesus/1222845 to your computer and use it in GitHub Desktop.
Classe para paginação no Diesel
<?php
$page = clear($_GET['page']);
if ($page == null) {
$page = 1;
}
$per_page = PER_PAGE;//Quantidade por página
$offset = ($page * $per_page) - $per_page;
$paginator = new Pager();
$paginator->table = "hm_pages";
$paginator->page = $page;
$paginator->path = anchor("admin/cms");
$paginator->per_page = $per_page;
$paginator->offset = $offset;
$pagination = $paginator->paginate();
$pages = Page::all($per_page,$offset, $search, $order);
<?php
class Pager {
public $table;
public $path;
public $page;
public $per_page;
public $offset;
public $conditionals;
public $operator = "AND";
public $total_pages;
public $prev;
public $next;
public function paginate($method = "default", $ajax_function = null) {
if ($this->conditionals != NULL) {
$this->conditionals = "WHERE " . join(" {$this->operator} ", $this->conditionals);
//Corrige o caminho da paginação
$this->path += join("&", $this->conditionals);
} else {
$this->conditionals = "";
}
$count = LDB::RunQuery("SELECT COUNT(*) as total FROM {$this->table} {$this->conditionals};");
list($total) = $count->fetch();
$this->total_pages = $total / $this->per_page;
$this->prev = $this->page - 1;
$this->next = $this->page + 1;
//criando link anterior
if ($this->page > 1) {
$prev_link = "<a href=\"$this->path?page=$this->prev\"> <span>&lsaquo;</span> anterior </a>";
if ($method == "ajax") {
$prev_link = "<a href=\"#\" onclick = \"$ajax_function($this->path, $this->prev); return false;\"> <span>&lsaquo;</span> anterior </a>";
}
} else {
$prev_link = "";
}
//Criando link próximo
if ($this->total_pages > $this->page) {
$next_link = "<a href=\"$this->path?page=$this->next\"> pr&oacute;ximo <span>&rsaquo;</span> </a> ";
if ($method == "ajax") {
$prev_link = "<a href=\"#\" onclick = \"$ajax_function($this->path, $this->next); return false;\"> <span>&rsaquo;</span> anterior </a>";
}
} else {
$next_link = "";
}
$this->total_pages = ceil($this->total_pages);
$last_page = $this->path."?page={$this->total_pages}";
$max = $this->page + 3;
//$max = 'limit' .($pag - 1) * $per_page .',' .$per_page;
if ($max >= $this->total_pages) {
$max = $this->total_pages;
}
if ($this->page >= 6) {
$value = $this->page;
} else {
$value = 1;
}
//criando o painel de navegação
for ($x = $value; $x <= $max; $x++) {
if ($x == $this->page) {
$panel .= "<li><a href=\"#\" class=\"current-page\"> $x</a></li> ";
} else {
$panel .= "<li> <a href=\"$this->path?page=$x\">$x</a>";
if ($method == "ajax") {
$panel = "<li><a href=\"#\" onclick = \"$ajax_function($this->path, $x); return false;\"> $x </a>";
}
}
}
return <<<PAG
<ul class="paginacao">
<li class="contador"> <a href="#"> Página: $this->page de $this->total_pages </a></li>
<li class="ant"> {$prev_link}</li>
{$panel} </li>
<li class="prox"> {$next_link} </li>
</ul>
PAG;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment