Ver Mensaje Individual
  #8 (permalink)  
Antiguo 15/04/2013, 12:15
emilkavg
 
Fecha de Ingreso: septiembre-2012
Ubicación: La Habana
Mensajes: 32
Antigüedad: 11 años, 7 meses
Puntos: 1
Respuesta: paginar resultados

AQUI TE DEJO MI CODIGO DE PAGINACION

************************************************

Código PHP:
Ver original
  1. <?php
  2. include ('../../includes/conexion.php');
  3. ?>
  4. <style type="text/css">
  5. /* --- ESTILO DEL PAGINADOR --------- */
  6. ul    { border:0; margin:0; padding:0; }
  7.  
  8. #style-pagination li          { border:0; margin:0; padding:0; font-size:11px; list-style:none; /* savers */ float:left; }
  9. #style-pagination a           { border:solid 1px #9aafe5; margin-right:2px; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px; }
  10. #style-pagination .previous-off,
  11. #style-pagination .next-off   { border:solid 1px #DEDEDE; color:#888888; display:block; float:left; font-weight:bold; margin-right:2px; padding:3px 4px; }
  12. #style-pagination .next a,
  13. #style-pagination .previous a { font-weight:bold; }
  14. #style-pagination .active     { background:#2e6ab1; color:#FFFFFF; font-weight:bold; display:block; float:left; padding:0px; /* savers */ margin-right:2px; }
  15. #style-pagination a:link,      
  16. #style-pagination a:visited   { color:#0e509e; display:block; float:left; padding:3px 6px; text-decoration:none; }
  17. #style-pagination a:hover     { border:solid 1px #0e509e; background:#2e6ab1; color:#FFFFFF; }
  18. /* --- FIN ESTILO DEL PAGINADOR --------- */
  19. </style>
  20. </head>
  21. <body bgcolor="#FFFFFF">
  22. <script language="JavaScript">
  23. function muestra(queCosa)
  24. {
  25.     alert(queCosa);
  26. }
  27. </script>
  28. <div id="targetBuscador" style="text-align: right;">
  29. <form action="general.php" method="get">
  30. Criterio de b&uacute;squeda:
  31. <input type="text" name="criterio" size="22" maxlength="150">
  32. <input type="submit" value="Buscar">
  33. <br>
  34. <a style="color:#999; font-size:10px;">(Para mostrar todos los registros, hacer clic en el bot&oacute;n buscar sin poner criterio de b&uacute;squeda)</a>
  35. </form>
  36. </div>
  37. <hr size='1'>
  38. <br>
  39. <?
  40. //INICIALIZO EL CRITERIO Y RECIBO CUALQUIER CADENA PARA BUSCAR
  41. $criterio = "";
  42. $txt_criterio = "";
  43. if ($_GET["criterio"]!=""){
  44.    $txt_criterio = $_GET["criterio"];
  45.    $criterio = " WHERE no_exp LIKE '%" . $txt_criterio . "%' or nombre LIKE '%" . $txt_criterio . "%' or primer_apellido LIKE '%" . $txt_criterio . "%' or segundo_apellido LIKE '%" . $txt_criterio . "%' or nit LIKE '%" . $txt_criterio . "%' ";
  46. }
  47.  
  48.  
  49. $sql="SELECT * FROM tenlibros.tb_titulares ".$criterio;
  50. $res=mysql_query($sql);
  51. $numeroRegistros=mysql_num_rows($res);
  52. if($numeroRegistros<=0)
  53. {
  54.     echo "<div align='center'>";
  55.     echo "<font face='verdana' size='-2'>No se encontraron resultados</font>";
  56.     echo "</div>";
  57. }else{
  58.  
  59.     //////////ELEMENTOS DE ORDEN
  60.     //Aqui por defecto se ordena por no_exp, pero al dar clic en el top de la columna el orden varia con relacion a esta
  61. $orden = "no_exp";
  62. if ($_GET["orden"]!=""){
  63.    $orden = $_GET["orden"];
  64.    $criterio = " WHERE no_exp LIKE '%" . $txt_criterio . "%' or nombre LIKE '%" . $txt_criterio . "%' or primer_apellido LIKE '%" . $txt_criterio . "%' or segundo_apellido LIKE '%" . $txt_criterio . "%' or nit LIKE '%" . $txt_criterio . "%' ";
  65. }
  66.        
  67.             //if(!isset($orden))
  68.             //{
  69.             //  $orden="no_exp";
  70.             //}
  71.    
  72.     //////////FIN -> DE ELEMENTOS DE ORDEN
  73.  
  74.     //////////CALCULO DE ELEMENTOS NECESARIOS PARA LA PAGINACION
  75.     //CANTIDAD DE ELEMENTOS POR PAGINA
  76.     $tamPag=20;
  77.  
  78.     //PAGINA ACTUAL -> SI NO ESTA DEFINIDA Y LIMITES
  79.     if(!isset($_GET["pagina"]))
  80.     {
  81.        $pagina=1;
  82.        $inicio=1;
  83.        $final=$tamPag;
  84.     }else{
  85.        $pagina = $_GET["pagina"];
  86.     }
  87.     //CALCULO DEL LIMITE INFERIOR
  88.     $limitInf=($pagina-1)*$tamPag;
  89.  
  90.     //CALCULO DEL NUMERO DE PAGINAS
  91.     $numPags=ceil($numeroRegistros/$tamPag);
  92.     if(!isset($pagina))
  93.     {
  94.        $pagina=1;
  95.        $inicio=1;
  96.        $final=$tamPag;
  97.     }else{
  98.        $seccionActual=intval(($pagina-1)/$tamPag);
  99.        $inicio=($seccionActual*$tamPag)+1;
  100.  
  101.        if($pagina<$numPags)
  102.        {
  103.           $final=$inicio+$tamPag-1;
  104.        }else{
  105.           $final=$numPags;
  106.        }
  107.  
  108.        if ($final>$numPags){
  109.           $final=$numPags;
  110.        }
  111.     }
  112.  
  113. //////////FIN -> CALCULO DEL NUMERO DE PAGINAS
  114.  
  115. //////////CREACION DE LA CONSULTA CON LIMITES
  116. $sql="SELECT * FROM tenlibros.tb_titulares ".$criterio." ORDER BY ".$orden." ASC LIMIT ".$limitInf.",".$tamPag;
  117. $res=mysql_query($sql);
  118. //////////FIN -> CONSULTA CON LIMITES
  119. echo "<div align='center'>";
  120. echo "<font face='verdana' size='-2'>| Encontrados: ".$numeroRegistros." resultados ";
  121.     if($orden=="no_exp"){ $verorden="No Expediente"; }
  122.     if($orden=="nombre"){ $verorden="Nombre y apellidos"; }
  123.     if($orden=="nit"){ $verorden="Nit"; }
  124. echo "| Ordenados por: ".$verorden." | ";
  125. if(isset($txt_criterio)){
  126.     if ($txt_criterio=="") { echo ""; }
  127.     if ($txt_criterio!="") { echo "Valor filtro: ".$txt_criterio." | "; }
  128. }
  129. echo "</font></div>";
  130. echo "<br>";
  131. echo "<hr size='1'>";
  132. echo "<table align='center' width='600' border='0' cellspacing='1' cellpadding='0'>";
  133. echo "<tr></tr>";
  134. echo "<td width='100'><b><a class='link' href='".$_SERVER["PHP_SELF"]."?pagina = ".$pagina."&orden=no_exp&criterio=".$txt_criterio."'>EXP</b></td>";
  135. echo "<td width='300'><b><a class='link' href='".$_SERVER["PHP_SELF"]."?pagina = ".$pagina."&orden=nombre&criterio=".$txt_criterio."'>NOMBRE Y APELLIDOS</b></td>";
  136. echo "<td width='150'><b><a class='link' href='".$_SERVER["PHP_SELF"]."?pagina = ".$pagina."&orden=nit&criterio=".$txt_criterio."'>NIT</a></b></td>";
  137. echo "<td width='50' class='link' align='center'><b>FICHA</b></td>";
  138. echo "<td width='50' class='link' align='center'><b>LIC.</b></td>";
  139. echo "<td width='50' class='link' align='center'><b>Trab.</b></td>";
  140. while($registro=mysql_fetch_array($res))
  141. {
  142. ?>
  143.    <!-- tabla de resultados -->
  144.     <tr class="tabla_contents">
  145.     <td><? echo $registro["no_exp"]; ?></td>
  146.     <td><? echo $registro["nombre"]; ?>&nbsp;<? echo $registro["primer_apellido"]; ?>&nbsp;<? echo $registro["segundo_apellido"]; ?></td>
  147.     <td><? echo $registro["nit"]; ?></td>
  148.     <td align="center"><a class="link" style="text-decoration:none;cursor:pointer;" onClick="show_titular(<?php echo $registro['id']; ?>)"><img src="../../imagenes/consultas/ico-titular.png" width="16" height="16" /></a></td>
  149.     <td align="center"><a class="link" style="text-decoration:none;cursor:pointer;" onClick="show_licencias(<?php echo $registro['id']; ?>)"><img src="../../imagenes/consultas/ico-licenc.png" width="16" height="16" /></a></td>
  150.     <td align="center"><a class="link" style="text-decoration:none;cursor:pointer;" onClick="show_trabcontratados(<?php echo $registro['id']; ?>)"><img src="../../imagenes/consultas/ico-trab.png" width="16" height="16" /></a></td>
  151.     </tr>
  152.    <!-- fin tabla resultados -->
  153. <?
  154. }//FIN -> WHILE
  155. echo "</table>";
  156. }//FIN -> IF
  157. //////////INICIO DE LA PAGINACION
  158. ?>
  159.     <br>
  160.     <table  id="style-pagination"  border="0" cellspacing="0" cellpadding="0" align="center">
  161.       <tr>
  162.         <td align="center" valign="top"><?
  163.     if($pagina>1)
  164.     {
  165.        echo "<a  href='".$_SERVER["PHP_SELF"]."?pagina=".($pagina-1)."&orden=".$orden."&criterio=".$txt_criterio."'>";
  166.        echo "ANTERIOR</a>";
  167.     }
  168.  
  169.     for($i=$inicio;$i<=$final;$i++)
  170.     {
  171.        if($i==$pagina)
  172.        {
  173.           //AQUI SE MUESTRA LA PAGINA ACTIVA
  174.           //echo "<a class=''><b>".$i."</b> </a>";
  175.        }else{
  176.           echo "<a  href='".$_SERVER["PHP_SELF"]."?pagina=".$i."&orden=".$orden."&criterio=".$txt_criterio."'>";
  177.           echo "<font face='verdana' size='-2'>".$i."</font></a> ";
  178.        }
  179.     }
  180.     if($pagina<$numPags)
  181.    {
  182.        echo "<a href='".$_SERVER["PHP_SELF"]."?pagina=".($pagina+1)."&orden=".$orden."&criterio=".$txt_criterio."'>";
  183.        echo "SIGUIENTE</a>";
  184.    }
  185. ////////// FIN  DE LA PAGINACION
  186. ?>
  187.         </td>
  188.       </tr>
  189.     </table>
  190. <br>
  191. <hr size='1'>
  192. <?php mysql_close(); ?>