Ver Mensaje Individual
  #2 (permalink)  
Antiguo 02/03/2021, 16:37
tuadmin
 
Fecha de Ingreso: abril-2006
Mensajes: 583
Antigüedad: 18 años
Puntos: 120
Respuesta: Problema de números en una paginación en pdo

te comparto una libreria que hice hace como 7 años y no recuerdo como la hice. haber si te sirve aun
paginator.class.php
Código PHP:
Ver original
  1. <?php /**
  2.  * clase simplificada
  3.  * -se agrego getNext and getPreview
  4.  * @version 2.7
  5.  * @author tuadmin
  6.  */
  7.  class paginator { private $a = 9; private $b = 0; private $c = 0; private $d = 9; private $e = 0; private $f; private $pageNext = 1; private $pagePreview = 1; static public function newInstance($g) { return new self($g); } public function __construct($g) { $this->c = $g; $this->f = new ArrayIterator(); } public function setTotalLinks($h) { $this->d = $h; return $this; } public function currentPage($page) { $this->b = $page < 0 ? 0:floor($page); return $this; } public function resultsPerPage($i = 9) { $this->a = $i; return $this; } public function paginate() { $this->e = ceil($this->c / $this->a); $this->e = $this->e; $this->b = ($this->b >= $this->e)?$this->e:$this->b ; $this->b = ($this->b < 1)?1:$this->b ; $j = ceil($this->d/2); $k = $this->d - $j; $l = $this->getBeforeCurrentPage($k); $m = $this->getAfterCurrentPage($j); $this->d ; if(($l + $this->d) > $this->e) { $l = ($this->e - $this->d) +1 ; $m = $l + $this->d; $l = $l < 1? 1:$l; } else { $m = $l + $this->d; } for($n = $l ;$n < $m && $n <= $this->e ; $n++) { $this->f->append($n); } $this->pagePreview = $this->getBeforeCurrentPage(1); $this->pageNext = $this->getAfterCurrentPage(1); return $this; } public function totalPages() { return $this->e; } public function currentPages() { return $this->f; } public function ArrayPages() { return $this->f; } public function getPages() { return $this->f; } public function limitSql() { return ("LIMIT ".($this->a * ($this->b-1)).",".$this->a); } public function __toString() { return 'LIMIT '.($this->a * ($this->b -1)).",".$this->a; } public function getLast() { $o = $this->getFirst() + $this->a; if($o > $this->c) { $o = $this->c - $this->getFirst(); $o = $o + $this->getFirst(); } return $o; } public function getFirst() { $p = ($this->a * ($this->b-1)); return $p; } public function getCurrentPage() { return $this->b; } public function getBeforeCurrentPage($q) { $r = $this->b - $q; if($r > 1) { return $r; } else { return 1; } } public function getAfterCurrentPage($q) { $s = $this->b + $q; if($s > $this->e) { return $this->e; } else { return $s; } } public function getPreview() { return $this->pagePreview; } public function getNext() { return $this->pageNext; } }

y mas bien tengo un texto de ejemplo
Código PHP:
Ver original
  1. /*
  2. $paginateSql = new paginator(10);
  3.         $paginateSql->paginator->resultsPerPage(1);
  4.         $paginateSql->currentPage(URL::GET('page'));
  5.         $paginateSql->paginate();
  6.         foreach ($paginateSql->ArrayPages() AS $page)
  7.         {
  8.             echo "  - $page -  ";
  9.         }
  10.  
  11.  
  12. $archivo = file("data4.txt");
  13. $lineas = count($archivo);
  14.  
  15. $actual = $_GET['actual'];
  16. $ejemplo = new paginator($lineas);
  17. //resultados por pagina
  18. $ejemplo->resultsPerPage(5);
  19. //pagina actual la que se esta viendo
  20. $ejemplo->currentPage($acual);
  21. //procesar todo para generar
  22. $ejemplo->paginate();
  23.  
  24. //rescatar resultado para mysql
  25. //esta parte lo modificas para usarlo en tu script osea modificas el 'metodo'
  26. $ejemplo->limitSql();
  27. /**/

asi que en teoria puedes probar haciendo COPY PASTE, pero recuerda leer cada linea, y trata de comprender el algoritmo


Código PHP:
Ver original
  1. <?php
  2. require('paginator.class.php');
  3. $registros = 20;
  4. $pagina = @$_GET['pagina'];
  5.  
  6. $pegar = "SELECT * FROM d_fich0s WHERE (fichas_fecha <= '$Fecha' AND fichas_fecha >= '$Fecha_dos') AND ficha_activa='1' AND ficha_tipo='0' ";
  7. $contarok        = $conexion->query($pegar);
  8. $total_registros = $contarok->rowCount();
  9.  
  10.  
  11. $ejemplo = new paginator($total_registros);
  12. //resultados por pagina
  13. $ejemplo->resultsPerPage(5);
  14.  
  15. //$ejemplo->setTotalLinks(4);//No recuerdo para q era esto
  16. //pagina actual la que se esta viendo
  17. $ejemplo->currentPage($pagina);
  18. //procesar todo para generar
  19. $ejemplo->paginate();//este debe ser el corazon de todo, supongo :v
  20.  
  21. //realizamos la busqueda en la base de datos
  22. $cad = $conexion->query($pegar. " $ordenaje ". $ejemplo->limitSql() ) or die('error al listar, $pegar' . PDOStatement::errorInfo());
  23.  
  24.  
  25. while ($row = $cad->fetch(PDO::FETCH_ASSOC)) {    
  26.     $tpl = CargarFile('tpls/noticias.item.tpl');    
  27.     $x .= '
  28.    <div class="notice">  
  29.  <div class="notice_title">      
  30.    <a title="' . str_replace('¿', '¿', latin1($row['ficha_titulo'])) . '" href="MK-ENLACE">' . str_replace('¿', '¿', latin1($row['ficha_titulo'])) . ' ' . cata($row['fichas_categoria'], $conexion) . '</a>  
  31.  </div>                                
  32.  <div class="clear">
  33.  </div>    
  34.  <div style="float:left;" class="hidden-xs">  
  35.    <div class="notice_tags"> ' . categorias_ver($row['fichas_categoria'], $conexion) . '
  36.    </div>  
  37.  </div>  
  38.  <div style="float:right;" class="hidden-xs">  
  39.    <div class="notice_tags"><b>Tags:</b> ' . Muestra_tags($row['fichas_tags'], $conexion) . '
  40.    </div>    
  41.  </div>    
  42.  <div style="clear:both;">
  43.  </div>    
  44.  <div class="jumpa">
  45.  </div>      
  46.  <div class="notice_image">    
  47.    <a title="' . str_replace('¿', '¿', latin1($row['ficha_titulo'])) . '" href="' . $_SERVER['SERVER_NAME'] . '/' . urls_amigables(latin1(cambiaresto($row['ficha_campo']))) . '/">
  48.      <img src="MK-IMAGENES"></a>  
  49.  </div>  
  50.  <div class="notice_description">   ' . bbcode(utf8_decode($row['ficha_descrip'])) . '  
  51.  </div>  
  52. </div>';
  53.    
  54.    
  55.     $x = str_replace('MK-IMAGENES', "/images-" . $row['ficha_id'] . "", $x);
  56.    
  57.     $x = str_replace('MK-DESCARGAS', $row['fichas_descargas'], $x);
  58.    
  59.     $ak_Rows = $x;
  60. }
  61.  
  62. //creando los enlaces de paginacion de resultados
  63.  
  64. $ak_Paginado_Paginas .= "<center><p>";
  65. $ak_Paginado_Paginas .= "<span class='pactiva'><a href='?pagina=" . $ejemplo->getPreview() . "'>&laquo; Anterior</a></span> ";
  66. foreach ($ejemplo->ArrayPages() AS $page)
  67. {
  68.     //echo "  - $page -  ";
  69.     if ($page == $pagina) {
  70.         $ak_Paginado_Paginas .= "<span class='pnumero'><b>" . $page . "</b></span> ";
  71.     } else {
  72.         $ak_Paginado_Paginas .= "<span class='pactiva'><a href='?pagina=$page'>$page</a></span> ";
  73.     }
  74. }
  75. $ak_Paginado_Paginas .= " <span class='pactiva'><a href='?pagina=" . $ejemplo->getNext() . "'>Siguiente &raquo;</a></span>";
  76. $ak_Paginado_Paginas .= "</p></center>";
  77.  
  78. ?>

y recordarte qe tienes fallo de seguridad si no Limpias de Inyeccion SQl las variables tipo $Fecha_dos,$ordenaje etc
__________________
Mis aportes te ayudaron??, te hicieron ahorrar valiosos tiempo??, si quieres puedes agradecerme con un Gracias o con una donacion
https://paypal.com/pools/c/8lmNLmWnG9