Ver Mensaje Individual
  #4 (permalink)  
Antiguo 14/03/2012, 13:07
Avatar de repara2
repara2
 
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Respuesta: Buscadpr mysql con mas de un campo?

Adáptalo a tu sistema pero la idea es esta:

Código PHP:
Ver original
  1. <?php
  2. ini_set('display_errors', 1);
  3.  
  4. if(isset($_POST['buscar']))//Porcesar sólo si se ha enviado algo por formulario
  5. {
  6.     $sql = "SELECT * FROM TABLA T WHERE 1 ";
  7.     //Nombre, si está definido
  8.     if(isset($_POST['nombre']) && $_POST['nombre']) {
  9.         $sql .="AND T.nombre LIKE '%{$_POST['nombre']}%' ";
  10.         echo "<h5>Nombre se parece a: {$_POST['nombre']}</h5>";
  11.     }
  12.  
  13.     //Género siempre está definido pero puede tener distintos valores, en este caso, sólo vale cuando
  14.     //es 0 o 1
  15.     if($_POST['genero']!="-1"){
  16.         $sql .="AND T.genero = '{$_POST['genero']}' ";
  17.         echo "<h5>El género es: {$_POST['genero']}</h5>";
  18.     }
  19.     else {
  20.         echo "<h5>No se ha definido el género</h5>";
  21.     }
  22.     //Calcular máximo y mńimo
  23.     if($_POST['min'] && $_POST['max'])
  24.     {
  25.         $sql .="AND (T.edad <= '{$_POST['max']}' AND T.edad >= '{$_POST['min']}') ";
  26.     }
  27.     elseif($_POST['min'] && !$_POST['max'])
  28.     {
  29.         $sql .="AND T.edad >= '{$_POST['min']}' ";
  30.  
  31.     }
  32.     elseif(!$_POST['min'] && $_POST['max'])
  33.     {
  34.         $sql .="AND T.edad <= '{$_POST['max']}' ";
  35.     }
  36.     echo "<h5>Mínimo es: {$_POST['min']}</h5>";
  37.     echo "<h5>Máximo es: {$_POST['max']}</h5>";
  38.     $sql .=";";
  39.    
  40.     echo "<h3>La select es: $sql</h3>";
  41.  
  42. }
  43. ?>
  44. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  45. <html xmlns="http://www.w3.org/1999/xhtml">
  46. <head>
  47. <meta http-equiv="Content-Type"
  48.     content="text/html; charset=windows-1252" />
  49. <title>Documento sin t&iacute;tulo</title>
  50. </head>
  51. <body>
  52.     <form id="form1" name="form1" method="post" action="">
  53.         <table width="300" border="0">
  54.             <tr>
  55.                 <td colspan="2"><div align="center">Buscador de personas</div></td>
  56.             </tr>
  57.             <tr>
  58.                 <td>G&eacute;nero</td><input name="buscar" type="hidden" id="buscar" size="1"
  59.                     maxlength="1" value="go" />
  60.                 <td><label> <select name="genero" id="genero">
  61.                             <option value="-1" selected="selected">Seleccione...</option>
  62.                             <option value="0">Masculino</option>
  63.                             <option value="1">Femenino</option>
  64.                     </select> </label></td>
  65.             </tr>
  66.             <tr>
  67.                 <td>Edad (min)</td>
  68.                 <td><input name="min" type="text" id="minimo" size="3"
  69.                     maxlength="3" /></td>
  70.             </tr>
  71.             <tr>
  72.                 <td>Edad (max)</td>
  73.                 <td><input name="max" type="text" id="maximo" size="3"
  74.                     maxlength="3" /></td>
  75.             </tr>
  76.             <tr>
  77.                 <td>Nombre</td>
  78.                 <td><label> <input name="nombre" type="text" id="nombre" size="20"
  79.                         maxlength="20" /> </label>
  80.                 </td>
  81.             </tr>
  82.             <tr>
  83.                 <td colspan="2"><input type="Submit"></td>
  84.             </tr>
  85.         </table>
  86.     </form>
  87. </body>
  88. </html>
__________________
Fere libenter homines, id quod volunt, credunt.