Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/02/2013, 20:59
awik
 
Fecha de Ingreso: febrero-2013
Mensajes: 78
Antigüedad: 11 años, 2 meses
Puntos: 0
Como Hacer links de paginación

Buenas , tengo una lista de usuarios
los cuales son los registrados , este se llama usuarios.php
al ingresar sale por orden de id todos los usuarios ...

como no quiero que salgan todos en una sola página le hise una paginación
para solo mostrar 5 usuarios por página ...

todo bien , pero necesito ayuda ,

necesito que el máximo de paginas sea solo 10

por ejemplo al costado le puse un boton llamado NEXT
yo quiero que cuando aprete NEXT se pase a otra página donde
empieze desde el numero 11 hasta el numero 20

por ejemplo que en la primera página sea asi ..

PREVIUS 1 - 2 - 3 - 4 -5 - 6 - 7- 8 - 9- 10 NEXT

cuando de NEXT , ahora saldran del número 11 hasta el numero 20

PREVIUS 11 - 12 - 13 - 14 -15 - 16 - 17- 18 - 19- 20 NEXT

y asi sucesivamente .. pero no lo logro



aqui pongo mi codigo PHP lo que hace listarlos y hace la paginación



Código PHP:
Ver original
  1. <?php
  2.    
  3.     require('connect_db.php');
  4.  
  5. // maximo por pagina
  6. $limit = 5;
  7.  
  8. // pagina pedida
  9. $pag = (int) $_GET["pag"];
  10. if ($pag < 1)
  11. {
  12.    $pag = 1;
  13. }
  14. $offset = ($pag-1) * $limit;
  15.  
  16.  
  17. $sql = "SELECT SQL_CALC_FOUND_ROWS id ,Nick ,Sexo ,FechaNac,DiaRegistro ,avatar FROM registro LIMIT $offset, $limit";
  18. $sqlTotal = "SELECT FOUND_ROWS() as total";
  19.  
  20. $rs = mysql_query($sql);
  21. $rsTotal = mysql_query($sqlTotal);
  22.  
  23. $num_rows=mysql_num_rows($rs);
  24.  
  25.  
  26. $rowTotal = mysql_fetch_assoc($rsTotal);
  27. // Total de registros sin limit
  28. $total = $rowTotal["total"];
  29.  
  30. while ($row = mysql_fetch_assoc($rs))
  31.          {
  32.            
  33.          
  34.      $id = $row["id"];
  35.      $Nick = $row["Nick"];
  36.      $Sexo = $row["Sexo"];
  37.      $Fecha = $row["FechaNac"];
  38.      $Registro = $row["DiaRegistro"];
  39.      $avatar = $row["avatar"];
  40.      ?>
  41.      <p></p>
  42.      
  43.      <td align ="center" Height="30" Width="150" background="http://i46.tinypic.com/qsur9t.jpg"><font color="red"><label for="foro">Usuario</label></font></td> <td align ="center" Height="30" Width="110" background="ImagenInicio/fondotabla3.jpg"></td>  <td align ="center" Height="30" Width="240" background="ImagenInicio/fondotabla3.jpg"></td>      <td align ="center" Height="30" Width="400" background="ImagenInicio/fondotabla3.jpg"></td>  </tr>  
  44.      
  45.      <tr>
  46.      <td align ="center" Height="110" Width="160" background="ImagenInicio/fondo.jpg" ><br><img src= <?php echo "miembros/$avatar" ?> width="110" height="130" /></br><br></br>Nick : <a href="perfil.php?id= <?php echo $id ?>"><font color="red"><?php echo $Nick ?></a></font><br></br></td>
  47.      <td align ="left" Height="110" Width="160" background="ImagenInicio/fondo.jpg"><br><?php echo "***" ?>Sexo :</br><br><?php echo "***" ?>Fecha de Nacimiento :</br><br><?php echo "***" ?>Se registro el :</br></td>  
  48.      <td align ="left" Height="30" Width="140" background="ImagenInicio/fondo.jpg"><br><?php echo "***" ?> <?php echo $Sexo ?></br><br><?php echo "***" ?><?php echo $Fecha ?></br><br><?php echo "***" ?><?php echo $Registro ?></br></td>    
  49.      <td align ="center" Height="30" Width="390" background="ImagenInicio/fondo.jpg"></td>  </tr>
  50.      
  51.                                                                                                  
  52.      </tr>
  53.      
  54.      
  55.      
  56.      <?php
  57.      }
  58.    
  59.  
  60. ?>     
  61. </table>
  62.  
  63.  
  64. <script type="text/javascript">
  65.     //Redireccionar con el formulario creado
  66.     document.formulario.submit();
  67. </script>
  68.    
  69.  
  70.    
  71.    
  72. <p>**</p>
  73. <p>**</p>
  74. </form>
  75. </body>
  76.  
  77.  
  78. <table border="0" Height="30" width="1400" cellpadding="0" cellspacing="0" align ="center">
  79. <tr>
  80. <td align="center" background="http://i46.tinypic.com/qsur9t.jpg"><font color=red >
  81.  
  82.  
  83.  
  84. <?php
  85.  
  86.  
  87.        
  88.          $totalPag = ceil($total/$limit);  
  89.          $links = array();
  90.          for( $i=1; $i<=$totalPag ; $i++)
  91.          {
  92.             $links[] = "<li><a href=\"?pag=$i\">$i</a></li>";
  93.          }
  94.          
  95.  
  96.          echo implode(" - ", $links);
  97.          
  98.       ?>
  99.       <li class="next"><a href="?page=8">Next »</a></li>
  100. </ul>
  101.  
  102.     </font>  
  103. </td>
  104. </tr>
  105. </table>


en la parte $totalPag = ceil($total/$limit)
es para sacar al numero total de páginas
$total q tiene todas las filas encontradas en la bd
y $limit que es la cantidad de registros que quiero que aparesca
la cual es 5
y con ceil hago que redondee al numero mas alto, el resultado de esto sale 3
asi que mostrara solo 3 páginas ..

mientras mas usuarios se registren el numero de páginas ira aumentando
yo quiero que solo llegue a un máximo de 10
y al darle next , continue del 11 al 2 , luego doy next y sera del 21 al 30 y asi sucesivamente...

espero alguien pueda ayudarme o darme alguna idea ....

gracias ..