Re: [PHP-ES] Problema con paginador

[email protected] (Luis Vega) Thu, 12 Nov 2015 09:51:27 -0300
Newsgroups php.general.es
Message-ID <CAKPF32A9E5kR1XP18mSji4-bDgL9pMpfJsYXAMCq7hqXF75iAA@mail.gmail.com>
--001a113fe84ee6aafd05245767eb
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Te respond=C3=AD hace muchos d=C3=ADas, ac=C3=A1 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=3D1, $per_page=3D20, $total_count=3D0)=
{
  $this->current_page =3D (int)$page;
         $this->per_page =3D (int)$per_page;
         $this->total_count =3D (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() >=3D 1 ? true : false;
   }

   public function has_next_page() {
return $this->next_page() <=3D $this->total_pages() ? true : false;
  }
}

?>



Ejemplo:

<?
require_once('paginator.inc.php');

$page =3D !empty($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page =3D 9; // Registros por pagina

$sql =3D "SELECT * FROM articulos";
$res =3D @mysql_query($sql,$dbh);
$total_count =3D @mysql_num_rows($res);
$pagination =3D new Pagination($page, $per_page, $total_count);

$sql .=3D " LIMIT ".$per_page;
$sql .=3D " OFFSET ".$pagination->offset();

$res =3D @mysql_query($sql,$dbh);

if(@mysql_num_rows($res) > 0){
while($row =3D 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=3D\"paginador\">";

          if($pagination->has_previous_page()) {
                echo "<a
href=3D'index.php?page=3D".$pagination->previous_page()."'>Anterior</a>";
          }

          for($i=3D1; $i <=3D $pagination->total_pages(); $i++) {
                if($i =3D=3D $page) {
echo "<strong>".$i."</strong>";
} else if($i =3D=3D $page-1 || $i =3D=3D $page-2 || $i =3D=3D $page-3 || $i=
 =3D=3D $page+1
|| $i =3D=3D $page+2 || $i =3D=3D $page+3){
echo "<a href=3D'".$paramPath."destacadas/".$i."'>".$i."</a>";
}
  }

          if($pagination->has_next_page()) {
echo "<a href=3D'index.php?page=3D".$pagination->next_page()."'>Siguiente</=
a> ";
}
echo "</div>";
    }
}

?>


Saludos


El 12 de noviembre de 2015, 9:46 a. m., Alexis Saucedo<
[email protected]> escribi=C3=B3:

> 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=C3=ADa 12 de noviembre de 2015, 6:17, Roberto Barreiro
> <[email protected]> escribi=C3=B3:
> >
> > El 12 de noviembre de 2015, 0:34, Alexis Saucedo <
> [email protected]>
> > escribi=C3=B3:
> >>
> >> gracias de antemano.
> >
> >
> > hace 5 a=C3=B1os atras, habia cientos de programadores en esta y en otr=
as
> > 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=C3=B1os que deje la programacion y e=
so que
> > hice proyectos muy grandes he importantes .... tendria que instalar tod=
as
> > las herramientas de programacion, LAMP, etc para revisar tu codigo y ve=
r
> > 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
>
>


--=20
Luis Vega Mart=C3=ADnez
Dise=C3=B1o y Desarrollo Web
Celular: +(56 9) 66787687
Web: www.crea7ive.cl

--001a113fe84ee6aafd05245767eb--