Re: Problema con paginador
| From: | Luis Vega | Date: | Thu, 12 Nov 2015 12:51:27 +0000 |
| Subject: | Re: Problema con paginador | ||
| References: | 1 2 3 | Groups: | php.general.es |
| Request: | Send a blank email to general-es+get-65715@lists.php.net to get a copy of this message | ||
Te respondí hace muchos días, acá va denuevo:
Utiliza esta clase:
<?php
// This is a helper class to make paginating records easy.
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public function __construct($page=1, $per_page=20, $total_count=0){
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
}
public function offset() {
// Assuming 20 items per page:
// page 1 has an offset of 0 (1-1) * 20
// page 2 has an offset of 20 (2-1) * 20
// in other words, page 2 starts with item 21
return ($this->current_page - 1) * $this->per_page;
}
public function total_pages() {
return ceil($this->total_count/$this->per_page);
}
public function previous_page() {
return $this->current_page - 1;
}
public function next_page() {
return $this->current_page + 1;
}
public function has_previous_page() {
return $this->previous_page() >= 1 ? true : false;
}
public function has_next_page() {
return $this->next_page() <= $this->total_pages() ? true : false;
}
}
?>
Ejemplo:
<?
require_once('paginator.inc.php');
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 9; // Registros por pagina
$sql = "SELECT * FROM articulos";
$res = @mysql_query($sql,$dbh);
$total_count = @mysql_num_rows($res);
$pagination = new Pagination($page, $per_page, $total_count);
$sql .= " LIMIT ".$per_page;
$sql .= " OFFSET ".$pagination->offset();
$res = @mysql_query($sql,$dbh);
if(@mysql_num_rows($res) > 0){
while($row = mysql_fetch_array($res)){
// Mostrar contenido
echo $row['nombre'];
}
}
?>
Mostrar paginador
<?php
if(@mysql_num_rows($res) > 0){
if($pagination->total_pages() > 1) {
echo "<div class=\"paginador\">";
if($pagination->has_previous_page()) {
echo "<a
href='index.php?page=".$pagination->previous_page()."'>Anterior</a>";
}
for($i=1; $i <= $pagination->total_pages(); $i++) {
if($i == $page) {
echo "<strong>".$i."</strong>";
} else if($i == $page-1 || $i == $page-2 || $i == $page-3 || $i == $page+1
|| $i == $page+2 || $i == $page+3){
echo "<a
href='".$paramPath."destacadas/".$i."'>".$i."</a>";
}
}
if($pagination->has_next_page()) {
echo "<a
href='index.php?page=".$pagination->next_page()."'>Siguiente</a>
";
}
echo "</div>";
}
}
?>
Saludos
El 12 de noviembre de 2015, 9:46 a. m., Alexis Saucedo<
alexissaucedo@gmail.com> escribió:
> Bueno roberto igual muy agradecido de el tiempo que te tomaste en
> contestar este email, segurile luchando con san google, jaj.
>
> Un gran saludo.
>
> El día 12 de noviembre de 2015, 6:17, Roberto Barreiro
> <rpbarreiro@gmail.com> escribió:
> >
> > El 12 de noviembre de 2015, 0:34, Alexis Saucedo <
> alexissaucedo@gmail.com>
> > escribió:
> >>
> >> gracias de antemano.
> >
> >
> > hace 5 años atras, habia cientos de programadores en esta y en otras
> > listas.... lamentablemente estan muertas desde hace ya mucho tiempo ...
> una
> > pena, yo aprendi mucho aqui ....y tambien me ayudaron mucho..
> >
> > lamento no poder ayudar, hace ya años que deje la programacion y eso que
> > hice proyectos muy grandes he importantes .... tendria que instalar todas
> > las herramientas de programacion, LAMP, etc para revisar tu codigo y ver
> > como se puede resolver y mejorar ...
> >
> > la verdad es que no se porque no me he dado de baja de esta lista aun
> .....
> > dicen que en google esta todo, pero no es asi.... hay cosas que solo la
> > experiencia te dan .....
> >
> > te tocara probar suerte en google y luchar en solitario ..... mucha
> suerte!!
> >
> >
>
> --
> PHP Spanish Localization Talk Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Luis Vega Martínez
Diseño y Desarrollo Web
Celular: +(56 9) 66787687
Web: www.crea7ive.cl