Re: Problemas con paginador

From: Date: Mon, 02 Nov 2015 01:55:35 +0000
Subject: Re: Problemas con paginador
References: 1  Groups: php.general.es 
Request: Send a blank email to general-es+get-65711@lists.php.net to get a copy of this message
El 1 de noviembre de 2015, 9:25 p. m., Alexis Saucedo< alexissaucedo@gmail.com> escribió: > Buenas noches a todos! les traigo una consulta que la verdad ya agote > recursos para resolverla, tengo un paginador que muestra ejemplo: > > <---anterior 1234 siguiente ---> > > el tema es que estoy buscando la forma de que no me muestre todos los > numeros de paginas si que los valla recorriendo, cuando tengo muchos > datos veo algo mas o menos asi: > > <---anterior 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 > 23 24 25 26 27 28 siguiente ---> > > y cuando hay mas de 500 datos es infernal, el codigo que estoy usando es > este: > > $query = "SELECT * FROM articulos"; > $result = mysqli_query($mysqli, $query); > // Count the total record > $total_records = mysqli_num_rows($result); > //Using ceil function to divide the total records on per page > $total_pages = ceil($total_records / $per_page); > > for ($i=1; $i<=$total_pages; $i++) { > if($i==$_GET['page']){ > $clase = 'active'; > }else{ > $clase = ''; > } > //numeros de paginas > $paginas[$i] = array('numero'=>$i,'clase'=>$clase); > }; > //array_push($articulos, 'clase', $clase); > if($total_pages==$_GET['page']){ > $siguiente = $total_pages; > }else{ > $siguiente = $_GET['page']+1; > } > if($_GET['page']=='1'){ > $anterior = '1'; > }else{ > $anterior = $_GET['page']-1; > } > > Alguien tiene idea como lo podria hacer?. > > Saludos y muchisimas gracias. > > -- > PHP Spanish Localization Talk Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > 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 -- Luis Vega Martínez Diseño y Desarrollo Web Celular: +(56 9) 66787687 Web: www.crea7ive.cl

« previous php.general.es (#65711) next »