Foros del Web » Programando para Internet » PHP »

Paginación PHP

Estas en el tema de Paginación PHP en el foro de PHP en Foros del Web. Problema con la paginacion en php he conseguido hacer que se muestre sólo tres artículos $query = mysql_query ("SELECT * FROM articulos LIMIT 0,4 ") ...
  #1 (permalink)  
Antiguo 06/07/2012, 02:01
 
Fecha de Ingreso: diciembre-2011
Mensajes: 414
Antigüedad: 12 años, 4 meses
Puntos: 1
Paginación PHP

Problema con la paginacion en php he conseguido hacer que se muestre sólo tres artículos

$query = mysql_query ("SELECT * FROM articulos LIMIT 0,4") or die(mysql_error);

Y el codigo que uso es

$query = mysql_query ("SELECT * FROM articulos LIMIT 0,4") or die(mysql_error);
while ($query_row = mysql_fetch_assoc($query)){
if ($row = mysql_fetch_array($query)){
echo " \n";
do {
echo "<h1>".$row['titulo']."</a></h1>";
echo "<br>";
echo "<h2>".$row['texto']."</a></h2>";
echo "<br>";
echo "<h3>".$row['alias']."</a>";
echo "<br>";

} while ($row = mysql_fetch_array($query));
echo "</table> \n";
} else {
echo "¡ No se ha encontrado ningún registro !";
}}

Y la paginación

$nro_reg=mysql_num_rows($query);
echo $nro_reg;
if($nro_reg=0){
echo 'No se han encontrado productos';
}

$nro_pagina=$_GET['num'];

$reg_por_pagina=3;

$can_paginas=$nro_reg/$reg_por_pagina;

La verdad es que stoy echo un lio con la paginación
  #2 (permalink)  
Antiguo 06/07/2012, 02:44
Avatar de cesarin3134  
Fecha de Ingreso: enero-2012
Ubicación: Milan
Mensajes: 97
Antigüedad: 12 años, 3 meses
Puntos: 20
Respuesta: Paginación PHP

puedes utilizar el highlight para visualizar tu codigo ??? se hace mas fatiga a leerlo asi
  #3 (permalink)  
Antiguo 06/07/2012, 03:10
Avatar de Eleazan  
Fecha de Ingreso: abril-2008
Ubicación: Ibiza
Mensajes: 1.879
Antigüedad: 16 años
Puntos: 326
Respuesta: Paginación PHP

Para paginar debes jugar con el limit....

Por ejemplo:

Mostrar los tres primeros resultados:

Código SQL:
Ver original
  1. LIMIT 0, 3

Mostrar la segunda página (del 3 al 6!)

Código SQL:
Ver original
  1. LIMIT 3, 3

La tercera página:

Código SQL:
Ver original
  1. LIMIT 6, 3


Si te fijas, LIMIT se usa de la siguiente manera

Código SQL:
Ver original
  1. LIMIT X, Y

Donde X es el registro por el que empieza, e Y es el número de registros a mostrar!

Así pues, con jugar un poco con el número de página, obtenemos

Código SQL:
Ver original
  1. LIMIT (NUMERO_PAGINA * REGISTROS_A_MOSTRAR - REGISTROS_A_MOSTRAR), REGISTROS_A_MOSTRAR

Más o menos, algo así ;)

Nota: Se resta "REGISTROS_A_MOSTRAR" al primer valor, pq normalmente empiezas por la "página 1", y seria 1 * 3 = 3... y deberia ser 0! Si para ti la primera página es 0, no debes restarlo! :)
Otra posibilidad es restar 1 al número de pagina!
__________________
>> Eleazan's Source
>> @Eleazan
  #4 (permalink)  
Antiguo 07/07/2012, 09:40
 
Fecha de Ingreso: diciembre-2011
Mensajes: 414
Antigüedad: 12 años, 4 meses
Puntos: 1
Respuesta: Paginación PHP

Es una buena solución , me he dado cuenta de que va a traves de limits por eso me he puesto ha investigar.

Ahora pregunto

Como puedo hacer que que se vean todos los articulos

Cita:
<?php
include("config.php");
$query = mysql_query ("SELECT * FROM articulos ORDER BY id_articulo DESC 0,3") or die(mysql_error);
while ($query_row = mysql_fetch_assoc($query)){
if ($row = mysql_fetch_array($query)){
echo " \n";
do {
echo "<h1>".$row['titulo']."</a></h1>";
echo "<br>";
echo "<h2>".$row['texto']."</a></h2>";
echo "<br>";
echo "<h3>".$row['alias']."</a></h3>";
} while ($row = mysql_fetch_array($query));
echo "</table> \n";
} else {
echo "¡ No se ha encontrado ningún registro !";
}}
?>
Lo digo porque cuando pongo ORDER BY id_articulo DESC el ultimo articulo no se muestra , y si pongo ASC tampoco , o sea que el primer registro no se muestra o el ultimo y no se pq.

Última edición por kfh1992; 07/07/2012 a las 10:12
  #5 (permalink)  
Antiguo 07/07/2012, 10:13
ruben_chirinos_1985
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Paginación PHP

Hola como estas, mira toma este script completo para una paginacion hasta tien un boton para imprimir adaptalo a lo q necesitas y listo, es decir los datos que quieres mostrar.

Código PHP:
Ver original
  1. <?php
  2.  
  3. $link = @mysql_connect("localhost", "root", "");
  4. mysql_select_db("proyec", $link);
  5.  
  6. // maximo por pagina
  7. $limit = 10;
  8. @$pag = 0;
  9. // pagina pedida
  10. @$pag =  $_GET["pag"];
  11. if ($pag < 1)
  12. {
  13.   @ $pag = 1;
  14. }
  15. $offset = (@$pag-1) * $limit;
  16.  
  17.  
  18. $sql = "SELECT SQL_CALC_FOUND_ROWS Ced_alumno, Nom_alumno, Ape_alumno, Direc_alumno, Telf, Fech_nac FROM alumnos ORDER BY Ced_alumno LIMIT $offset, $limit ";
  19. $sqlTotal = "SELECT FOUND_ROWS() as total";
  20.  
  21. $rs = mysql_query($sql);
  22. $rsTotal = mysql_query($sqlTotal);
  23.  
  24. $rowTotal = mysql_fetch_assoc($rsTotal);
  25. // Total de registros sin limit
  26. $total = $rowTotal["total"];
  27.  
  28. ?>
  29. <style type="text/css">
  30. <!--
  31. .Estilo3 {font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; }
  32. .Estilo6 {
  33.     font-family: Geneva, Arial, Helvetica, sans-serif;
  34.     font-size: 14px;
  35.     font-weight: bold;
  36. }
  37. .Estilo4 {font-size: 12px;
  38.     font-weight: bold;
  39. }
  40. .Estilo7 {font-size: 12px}
  41. .Estilo14 {font-family: "Times New Roman", Times, serif; font-size: 14px; font-weight: bold; color: #FFFFFF; }
  42. a:link {
  43.     color: #0000FF;
  44.     text-decoration: none;
  45. }
  46. a:visited {
  47.     color: #0000FF;
  48.     text-decoration: none;
  49. }
  50. a:hover {
  51.     color: #0000FF;
  52.     text-decoration: none;
  53. }
  54. a:active {
  55.     color: #0000FF;
  56.     text-decoration: none;
  57. }
  58. -->
  59. </style>
  60.  
  61.  
  62.  
  63. <table width="703" border="0" align="center">
  64.   <tr>
  65.     <td width="169"><img src="../Imagenes/rbv.png" width="169" height="62" /></td>
  66.     <td width="80"><strong><img src="../Imagenes/logome.jpg" width="79" height="43" /></strong></td>
  67.     <td width="143"><span class="Estilo4">Ministerio del      Poder Popular para la Educaci&oacute;n</span></td>
  68.     <td width="72"><img src="../Imagenes/logoliceo.jpg" width="72" height="50" /></td>
  69.     <td width="217"><span class="Estilo7">LICEO BOLIVARIANO<br />
  70.           <strong>&ldquo;Rafael &Aacute;ngel Rond&oacute;n M&aacute;rquez&rdquo;</strong></span></td>
  71.   </tr>
  72.   <tr></tr>
  73.   <tr></tr>
  74.   <tr></tr>
  75. </table>
  76. <p align="center" class="Estilo6">Listado de Alumnos por P&aacute;ginas </p>
  77. <table width="683" border="1" align="center" bordercolor="#000" bgcolor="#FFFFFF">
  78.   <thead>
  79.     <tr>
  80.       <td width="59" bgcolor="#14285f"><div align="center"><span class="Estilo14">C&eacute;dula</span></div></td>
  81.       <td width="103" bgcolor="#14285f"><div align="center"><span class="Estilo14">Nombres</span></div></td>
  82.       <td width="130" bgcolor="#14285f"><div align="center"><span class="Estilo14">Apellidos</span></div></td>
  83.       <td width="219" bgcolor="#14285f"><div align="center"><span class="Estilo14">Direccion</span></div></td>
  84.       <td width="59" bgcolor="#14285f"><div align="center"><span class="Estilo14">Telefono</span></div></td>
  85.       <td width="73" bgcolor="#14285f"><div align="center"><span class="Estilo14">F/Nacimiento</span></div></td>
  86.     </tr>
  87.   </thead>
  88.   <tbody>
  89.     <?php
  90.          while ($row = mysql_fetch_assoc($rs))
  91.          {
  92.             $Ced_alumno = $row["Ced_alumno"];
  93.             $Nom_alumno = $row["Nom_alumno"];
  94.             $Ape_alumno = $row["Ape_alumno"];
  95.             $Direc_alumno = $row["Direc_alumno"];
  96.             $Telf = $row["Telf"];
  97.             $Fech_nac = htmlentities($row["Fech_nac"]);
  98.          ?>
  99.     <tr>
  100.       <td><span class="Estilo3"><?php echo $Ced_alumno; ?></span></td>
  101.       <td><span class="Estilo3"><?php echo $Nom_alumno; ?></span></td>
  102.       <td><span class="Estilo3"><?php echo $Ape_alumno; ?></span></td>
  103.       <td><span class="Estilo3"><?php echo $Direc_alumno; ?></span></td>
  104.       <td><span class="Estilo3"><?php echo $Telf; ?></span></td>
  105.       <td><span class="Estilo3"><?php echo $Fech_nac; ?></span></td>
  106.     </tr>
  107.     <?php
  108.          }
  109.       ?>
  110.   </tbody>
  111.   <tfoot>
  112.     <tr>
  113.       <td colspan="6" bgcolor="#FFFFFF"><div align="center"><span class="Estilo3"><strong>P&aacute;gina:</strong>
  114.                 <?php
  115.          $totalPag = ceil($total/$limit);
  116.          $links = array();
  117.          for( $i=1; $i<=$totalPag ; $i++)
  118.          {
  119.             $links[] = "<a href=\"?pag=$i\">$i</a>";  
  120.          }
  121.          echo implode(" - ", $links);
  122.       ?>
  123.       </span><span></span><span></span></div></td>
  124.     </tr>
  125.   </tfoot>
  126. </table>
  127. <p align="center"><a href="inicio.php">Volver</a></p>
  128.   <div align="center">
  129.     <?php
  130.   $strPrint="Imprimir";
  131. echo '<br /><br />&nbsp;<input type="button" style="visibility: ; width: 60px; height: 25px" id="print" value="' . $strPrint . '" onclick="printPage()">' . "\n";
  132. ?>
  133.   </div>
  134.   <p align="center"> <script type="text/javascript" language="javascript1.2">
  135. <!--
  136. function printPage()
  137. {
  138.     document.getElementById('print').style.visibility = 'hidden';
  139.     // Do print the page
  140.     if (typeof(window.print) != 'undefined') {
  141.         window.print();
  142.     }
  143.     document.getElementById('print').style.visibility = '';
  144. }
  145. //-->
  146.   </script>
  #6 (permalink)  
Antiguo 07/07/2012, 15:22
 
Fecha de Ingreso: diciembre-2011
Mensajes: 414
Antigüedad: 12 años, 4 meses
Puntos: 1
Respuesta: Paginación PHP

Este script me ha ido de perlas porque muestran los articulos y tres pero la paginación no se hace xd

jja

Haber
Cita:
<table width="683" border="1" align="center" bordercolor="#000" bgcolor="#FFFFFF">
<tbody>
<?php
while ($row = mysql_fetch_assoc($rs))
{
$id_art = $row["id_articulo"];
$titulo = $row["titulo"];
$texto = $row["texto"];
$alias = $row["alias"];
?>
<tr>
<td><span class="Estilo3"><?php echo $id_art; ?></span></td>
<td><span class="Estilo3"><?php echo $titulo; ?></span></td>
<td><span class="Estilo3"><?php echo $texto; ?></span></td>
<td><span class="Estilo3"><?php echo $alias ?></span></td>
</tr>
<?php
}
?>
</tbody>
</table>
Esto funciona correctamente

La parte de la paginacion se muestran los tres registros,pero la paginación no funciona TT'

Cita:
<?php
$link = @mysql_connect("$server", "$db_user", "$db_pass");
mysql_select_db("$db_base", $link);
// maximo por pagina
$limit = 3;
$pag = 2;
// pagina pedida
$pag = $_GET["total"];
if ($pag < 1)
{
$pag = 1;
}
$offset = ($pag-1) * $limit;
$sql = "SELECT * FROM articulos ORDER BY id_articulo LIMIT $offset, $limit ";
$sqlTotal = "SELECT FOUND_ROWS() as total";
$rs = mysql_query($sql);
$rsTotal = mysql_query($sqlTotal);
$rowTotal = mysql_fetch_assoc($rsTotal);
// Total de registros sin limit
$total = $rowTotal["pag"];
?>
  #7 (permalink)  
Antiguo 07/07/2012, 18:00
ruben_chirinos_1985
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Paginación PHP

Hola a simple vista se puede ver que en el array tienes

$id_art = $row["id_articulo"];

y en el echo tienes

<td><span class="Estilo3"><?php echo $id_art; ?></span></td>

tiens q ser igual como lo llamas de l array es decir igual como esta en la tabla para q te evites problemas.
  #8 (permalink)  
Antiguo 08/07/2012, 05:58
 
Fecha de Ingreso: diciembre-2011
Mensajes: 414
Antigüedad: 12 años, 4 meses
Puntos: 1
Respuesta: Paginación PHP

Mira tal y como te lo digo , el resultado es el siguiente

http://img77.xooimage.com/files/e/f/...do-362a10d.jpg

Así que creo que no me has entendido la pregunta que te he dicho,es que esta parte

Cita:
<?php
$link = @mysql_connect("$server", "$db_user", "$db_pass");
mysql_select_db("$db_base", $link);
// maximo por pagina
$limit = 3;
$pag = 0;
// pagina pedida
$pag = $_GET["total"];
if ($pag < 1)
{
$pag = 1;
}
$offset = ($pag-1) * $limit;
$sql = "SELECT * FROM articulos ORDER BY id_articulo LIMIT $offset, $limit ";
$sqlTotal = "SELECT FOUND_ROWS() as total";
$rs = mysql_query($sql);
$rsTotal = mysql_query($sqlTotal);
$rowTotal = mysql_fetch_assoc($rsTotal);
// Total de registros sin limit
$total = $rowTotal["pag"];
?>
No se prepararla TT'
  #9 (permalink)  
Antiguo 12/07/2012, 02:27
 
Fecha de Ingreso: diciembre-2011
Mensajes: 414
Antigüedad: 12 años, 4 meses
Puntos: 1
Respuesta: Paginación PHP

Lo he conseguido ...una parte pero almenos algo es algo

Entonces como podria hacer para que en la pagina 1 salieran solo 2 articulos en la otra otros dos y asi sucesivamente
Cita:
<?php
// maximo por pagina
$limit = 10;
@$pag = 0;
// pagina pedida
@$pag = $_GET["pag"];
if ($pag < 1)
{
@ $pag = 1;
}
$offset = (@$pag-1) * $limit;


$sql = "SELECT * FROM articulos";
$sqlTotal = "SELECT FOUND_ROWS() as total";

$rs = mysql_query($sql);
$rsTotal = mysql_query($sqlTotal);

$rowTotal = mysql_fetch_assoc($rsTotal);
// Total de registros sin limit
$total = $rowTotal["total"];

?>
<style type="text/css">
<!--
.Estilo3 {font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; }
.Estilo6 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
}
.Estilo4 {font-size: 12px;
font-weight: bold;
}
.Estilo7 {font-size: 12px}
.Estilo14 {font-family: "Times New Roman", Times, serif; font-size: 14px; font-weight: bold; color: #FFFFFF; }
a:link {
color: #0000FF;
text-decoration: none;
}
a:visited {
color: #0000FF;
text-decoration: none;
}
a:hover {
color: #0000FF;
text-decoration: none;
}
a:active {
color: #0000FF;
text-decoration: none;
}
-->
</style>



<table width="703" border="0" align="center">
<tr>
<td width="169"><img src="http://www.forosdelweb.com/f18/Imagenes/rbv.png" width="169" height="62" /></td>
<td width="80"><strong><img src="http://www.forosdelweb.com/f18/Imagenes/logome.jpg" width="79" height="43" /></strong></td>
<td width="143"><span class="Estilo4">Ministerio del Poder Popular para la Educaci&oacute;n</span></td>
<td width="72"><img src="http://www.forosdelweb.com/f18/Imagenes/logoliceo.jpg" width="72" height="50" /></td>
<td width="217"><span class="Estilo7">LICEO BOLIVARIANO<br />
<strong>&ldquo;Rafael &Aacute;ngel Rond&oacute;n M&aacute;rquez&rdquo;</strong></span></td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
</table>
<p align="center" class="Estilo6">Listado de Alumnos por P&aacute;ginas </p>
<table width="683" border="1" align="center" bordercolor="#000" bgcolor="#FFFFFF">
<thead>
<tr>
<td width="59" bgcolor="#14285f"><div align="center"><span class="Estilo14">C&eacute;dula</span></div></td>
<td width="103" bgcolor="#14285f"><div align="center"><span class="Estilo14">Nombres</span></div></td>
<td width="130" bgcolor="#14285f"><div align="center"><span class="Estilo14">Apellidos</span></div></td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_assoc($rs))
{
$Ced_alumno = $row["texto"];
$Nom_alumno = $row["titulo"];
$Ape_alumno = $row["alias"];
?>
<tr>
<td><span class="Estilo3"><?php echo $Ced_alumno; ?></span></td>
<td><span class="Estilo3"><?php echo $Nom_alumno; ?></span></td>
<td><span class="Estilo3"><?php echo $Ape_alumno; ?></span></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<td colspan="6" bgcolor="#FFFFFF"><div align="center"><span class="Estilo3"><strong>P&aacute;gina:</strong>
<?php
$totalPag = ceil($total/$limit);
$links = array();
for( $i=1; $i<=$totalPag ; $i++)
{
$links[] = "<a href=\"?pag=$i\">$i</a>";
}
echo implode(" - ", $links);
?>
</span><span></span><span></span></div></td>
</tr>
</tfoot>
</table>
<p align="center"><a href="index.php">Vo
lver</a></p>

Etiquetas: mysql, registro, sql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:57.