Foros del Web » Programando para Internet » PHP »

Filtrado de registros en tabla php y mysql

Estas en el tema de Filtrado de registros en tabla php y mysql en el foro de PHP en Foros del Web. Hola amigos. Conseguí un código para el filtrado de datos de una tabla php y mysql y me funciona bien las opciones de filtrado pero ...
  #1 (permalink)  
Antiguo 27/11/2012, 07:16
Avatar de kimmy  
Fecha de Ingreso: julio-2008
Mensajes: 841
Antigüedad: 15 años, 9 meses
Puntos: 15
Pregunta Filtrado de registros en tabla php y mysql

Hola amigos. Conseguí un código para el filtrado de datos de una tabla php y mysql y me funciona bien las opciones de filtrado pero no me funciona cuando quiero ver todos los datos. El problema es que al pasar una variable que es la que hace el filtro, no me muestra cuando es todos los datos. Les muestro mi código:

Aqui está el archivo de la tabla:

Código PHP:
Ver original
  1. <body>
  2.     <div id="top">
  3.     </div>
  4.  
  5.     <div id="content">
  6.         <div class="filtro">
  7.             <form id="frm_filtro" method="post" action="">
  8.                
  9.               <ul>
  10.                <li>
  11.                <label>Estatus:</label>
  12.                        <select name="estatus">
  13.                             <option value="0">-------------------------</option>
  14.                             <!-- Listar Paises -->
  15.                             <?php
  16.                             $query = mysql_query("SELECT DISTINCT id_reservacion, estatus FROM reservaciones WHERE estatus='Verificando disponibilidad' or estatus='Pago pendiente' GROUP BY estatus");
  17.                             while($row = mysql_fetch_array($query)){
  18.                                 $id_res = $row['id_reservacion'];
  19.                                 ?>
  20.                                
  21.                                 <option value="<?php echo $row['estatus'] ?>">
  22.                                 <?php echo $row['estatus'] ?>
  23.                                 </option>
  24.                             <?php
  25.                             }
  26.                             ?>             
  27.                        </select>
  28.                         <input type="hidden" name="vid" value="<?php echo $id_res ?>" />    
  29.                         </li>
  30.                         <li>
  31.                    <button type="button" id="btnfiltrar">Filtrar</button>
  32.                     </li>
  33.                      <li>
  34.                         <a href="javascript:;" id="btncancel">Todos</a>
  35.                     </li>
  36.                 </ul>
  37.             </form>
  38.         </div>
  39.         <table cellpadding="0" cellspacing="0" id="data">
  40.             <thead>
  41.                 <tr>
  42.                     <th width="49"><span title="id_reservacion">Id</span></th>
  43.                     <th width="128"><span title="n_reservacion">Nº Reservacion</span></th>
  44.                     <th width="97"><span title="fecha">Fecha</span></th>
  45.                     <th width="92"><span title="nombre">Nombre</span></th>
  46.                     <th width="88"><span title="letra_ci">Cedula</span></th>
  47.                     <th width="83"><span title="nombre_posada">Posada</span></th>
  48.                     <th width="96"><span title="nombre_plan">Plan</span></th>
  49.                     <th width="68"><span title="desde">Desde</span></th>
  50.                     <th width="68"><span title="hasta">Hasta</span></th>
  51.                     <th width="92"><span title="estatus">Estatus</span></th>
  52.                     <th width="77"><span title="asignar">Asignar</span></th>
  53.                 </tr>
  54.             </thead>
  55.             <tbody>
  56.             </tbody>
  57.         </table>
  58.     </div>    
  59. </body>

aqui el ajax.php

Código PHP:
Ver original
  1. include (func.php');
  2.  $conex = db_connect();
  3.  if (!$conex)
  4.   return 0;
  5.  
  6. if($_GET['action'] == 'listar')
  7. {
  8.     // valores recibidos por POST
  9.     $vestatus = $_POST['estatus'];
  10.     $vid = $_POST['vid'];
  11.    
  12.     $sql = "SELECT * FROM reservaciones WHERE estatus='$vestatus'";
  13.                                        
  14.     // Vericamos si hay algun filtro
  15.     $sql .= ($vestatus > 0)   ? " AND estatus = '".$vestatus."'" : "";
  16.    
  17.     // Ordenar por
  18.     $vorder = $_POST['orderby'];
  19.    
  20.     if($vorder != ''){
  21.         $sql .= " ORDER BY ".$vorder;
  22.     }
  23.    
  24.     $query = mysql_query($sql);
  25.     $datos = array();
  26.  
  27.     while($row = mysql_fetch_array($query))
  28.     {
  29.         $datos[] = array(
  30.             'id_reservacion'    => $row['id_reservacion'],
  31.             'n_reservacion'     => $row['n_reservacion'],
  32.             'fecha_a'           => $row['fecha_carga'],
  33.             $fecha_o = $row['fecha_carga'],
  34.             $fecha = substr($fecha_o, 0,-9),
  35.             'fecha'             => $fecha,
  36.             'nombre'            => $row['nombre'],
  37.             'letraci'           => $row['letraci']. '-'.$row['ci'] ,
  38.             'nombre_posada'     => $row['nombre_posada'],
  39.             'nombre_plan'       => $row['nombre_plan'],
  40.             'desde'             => $row['llegada'],
  41.             'hasta'             => $row['salida'],
  42.             'estatus'           => $row['estatus'],
  43.         );
  44.     }
  45.     // convertimos el array de datos a formato json
  46.     echo json_encode($datos);
  47. }

aqui el js:

Código Javascript:
Ver original
  1. // JavaScript Document
  2. var ordenar = '';
  3. $(document).ready(function(){
  4.    
  5.     // Llamando a la funcion de busqueda al cargar la pagina
  6.     filtrar()
  7.    
  8.     var dates = $( "#del, #al" ).datepicker({
  9.             yearRange: "-50",
  10.             defaultDate: "+1w",
  11.             changeMonth: true,
  12.             changeYear: true,
  13.             onSelect: function( selectedDate ) {
  14.                 var option = this.id == "del" ? "minDate" : "maxDate",
  15.                     instance = $( this ).data( "datepicker" ),
  16.                     date = $.datepicker.parseDate(
  17.                         instance.settings.dateFormat ||
  18.                         $.datepicker._defaults.dateFormat,
  19.                         selectedDate, instance.settings );
  20.                 dates.not( this ).datepicker( "option", option, date );
  21.             }
  22.     });
  23.    
  24.     // filtrar al darle click al boton
  25.     $("#btnfiltrar").click(function(){ filtrar() });
  26.    
  27.     // boton cancelar
  28.     $("#btncancel").click(function(){
  29.         $(".filtro input").val('')
  30.         $(".filtro select").find("option[value='0']").attr("selected",true)
  31.         filtrar()
  32.     });
  33.    
  34.     // ordenar por
  35.     $("#data th span").click(function(){
  36.         var orden = '';
  37.         if($(this).hasClass("desc"))
  38.         {
  39.             $("#data th span").removeClass("desc").removeClass("asc")
  40.             $(this).addClass("asc");
  41.             ordenar = "&orderby="+$(this).attr("title")+" asc"     
  42.         }else
  43.         {
  44.             $("#data th span").removeClass("desc").removeClass("asc")
  45.             $(this).addClass("desc");
  46.             ordenar = "&orderby="+$(this).attr("title")+" desc"
  47.         }
  48.         filtrar()
  49.     });
  50. });
  51.  
  52. function filtrar()
  53. {  
  54.     $.ajax({
  55.         data: $("#frm_filtro").serialize()+ordenar,
  56.         type: "POST",
  57.         dataType: "json",
  58.         url: "ajax.php?action=listar",
  59.             success: function(data){
  60.                 var html = '';
  61.                 if(data.length > 0){
  62.                     $.each(data, function(i,item){
  63.                         html += '<tr>'
  64.                             html += '<td>'+item.id_reservacion+'</td>'
  65.                             html += '<td>'+item.n_reservacion+'</td>'
  66.                             html += '<td>'+item.fecha+'</td>'
  67.                             html += '<td>'+item.nombre+'</td>'
  68.                             html += '<td>'+item.letraci+'</td>'
  69.                             html += '<td>'+item.nombre_posada+'</td>'
  70.                             html += '<td>'+item.nombre_plan+'</td>'
  71.                             html += '<td>'+item.desde+'</td>'  
  72.                             html += '<td>'+item.hasta+'</td>'  
  73.                             html += '<td>'+item.estatus+'</td>'
  74.                             html += '<td><a href="asignar_estatus_reserva.php?id_reservacion='+item.id_reservacion+'">Detalle</a></td>'                                                
  75.                        
  76.                                                            
  77.                     });                
  78.                 }
  79.                 if(html == '') html = '<tr><td colspan="4" align="center">No se encontraron registros..</td></tr>'
  80.                 $("#data tbody").html(html);
  81.             }
  82.       });
  83. }

Como puedo hacer que también muestre el listado completo?????

Gracias
__________________
Caminando con el corazón partío

Etiquetas: html, mysql, registro, registros, sql, tabla, variables, filtros
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 14:13.