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<?php /**
* clase simplificada
* -se agrego getNext and getPreview
* @version 2.7
* @author tuadmin
*/
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/*
$paginateSql = new paginator(10);
$paginateSql->paginator->resultsPerPage(1);
$paginateSql->currentPage(URL::GET('page'));
$paginateSql->paginate();
foreach ($paginateSql->ArrayPages() AS $page)
{
echo " - $page - ";
}
$archivo = file("data4.txt");
$lineas = count($archivo);
$actual = $_GET['actual'];
$ejemplo = new paginator($lineas);
//resultados por pagina
$ejemplo->resultsPerPage(5);
//pagina actual la que se esta viendo
$ejemplo->currentPage($acual);
//procesar todo para generar
$ejemplo->paginate();
//rescatar resultado para mysql
//esta parte lo modificas para usarlo en tu script osea modificas el 'metodo'
$ejemplo->limitSql();
/**/
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<?php
require('paginator.class.php');
$registros = 20;
$pagina = @$_GET['pagina'];
$pegar = "SELECT * FROM d_fich0s WHERE (fichas_fecha <= '$Fecha' AND fichas_fecha >= '$Fecha_dos') AND ficha_activa='1' AND ficha_tipo='0' ";
$contarok = $conexion->query($pegar);
$total_registros = $contarok->rowCount();
$ejemplo = new paginator($total_registros);
//resultados por pagina
$ejemplo->resultsPerPage(5);
//$ejemplo->setTotalLinks(4);//No recuerdo para q era esto
//pagina actual la que se esta viendo
$ejemplo->currentPage($pagina);
//procesar todo para generar
$ejemplo->paginate();//este debe ser el corazon de todo, supongo :v
//realizamos la busqueda en la base de datos
$cad = $conexion->query($pegar. " $ordenaje ". $ejemplo->limitSql() ) or
die('error al listar, $pegar' . PDOStatement
::errorInfo());
while ($row = $cad->fetch(PDO::FETCH_ASSOC)) {
$tpl = CargarFile('tpls/noticias.item.tpl');
$x .= '
<div class="notice">
<div class="notice_title">
<a title="' . str_replace('¿', '¿', latin1
($row['ficha_titulo'])) . '" href="MK-ENLACE">' . str_replace('¿', '¿', latin1
($row['ficha_titulo'])) . ' ' . cata
($row['fichas_categoria'], $conexion) . '</a> </div>
<div class="clear">
</div>
<div style="float:left;" class="hidden-xs">
<div class="notice_tags"> ' . categorias_ver($row['fichas_categoria'], $conexion) . '
</div>
</div>
<div style="float:right;" class="hidden-xs">
<div class="notice_tags"><b>Tags:</b> ' . Muestra_tags($row['fichas_tags'], $conexion) . '
</div>
</div>
<div style="clear:both;">
</div>
<div class="jumpa">
</div>
<div class="notice_image">
<a title="' . str_replace('¿', '¿', latin1
($row['ficha_titulo'])) . '" href="' . $_SERVER['SERVER_NAME'] . '/' . urls_amigables
(latin1
(cambiaresto
($row['ficha_campo']))) . '/"> <img src="MK-IMAGENES"></a>
</div>
<div class="notice_description"> ' . bbcode
(utf8_decode($row['ficha_descrip'])) . ' </div>
</div>';
$x = str_replace('MK-IMAGENES', "/images-" . $row['ficha_id'] . "", $x);
$x = str_replace('MK-DESCARGAS', $row['fichas_descargas'], $x);
$ak_Rows = $x;
}
//creando los enlaces de paginacion de resultados
$ak_Paginado_Paginas .= "<center><p>";
$ak_Paginado_Paginas .= "<span class='pactiva'><a href='?pagina=" . $ejemplo->getPreview() . "'>« Anterior</a></span> ";
foreach ($ejemplo->ArrayPages() AS $page)
{
//echo " - $page - ";
if ($page == $pagina) {
$ak_Paginado_Paginas .= "<span class='pnumero'><b>" . $page . "</b></span> ";
} else {
$ak_Paginado_Paginas .= "<span class='pactiva'><a href='?pagina=$page'>$page</a></span> ";
}
}
$ak_Paginado_Paginas .= " <span class='pactiva'><a href='?pagina=" . $ejemplo->getNext() . "'>Siguiente »</a></span>";
$ak_Paginado_Paginas .= "</p></center>";
?>
y recordarte qe tienes fallo de seguridad si no Limpias de Inyeccion SQl las variables tipo $Fecha_dos,$ordenaje etc