Ver Mensaje Individual
  #9 (permalink)  
Antiguo 29/05/2011, 11:59
charlyta
 
Fecha de Ingreso: junio-2008
Mensajes: 291
Antigüedad: 15 años, 10 meses
Puntos: 9
Respuesta: optimizar código

Código PHP:
Ver original
  1. <?php
  2. function buscar($Tabla,$Campos,$db,$con){
  3.     $numero = count($_GET);
  4.     $tags = array_keys($_GET);
  5.     $valores = array_values($_GET);
  6.     for($i=0;$i<$numero;$i++)
  7.     {
  8.         $$tags[$i]=$valores[$i];
  9.     }
  10. foreach ($_GET as $key => $value) if ($key != ''){
  11.       $preq .= " AND ( 0";
  12.     foreach($Campos as $Campo)$preq .= " OR $Campo LIKE '%".$value."%'";
  13.     $preq .= ")";
  14.    
  15.   }
  16.   $result=mysql_query("SELECT * FROM '$Tabla' WHERE $preq",$con);
  17.          
  18. }
  19. ?>
  20. <html><head>
  21. <title>Buscando ...</title>
  22. </head><body><?
  23. ?>
  24. <form method="GET" action="recibir.php">
  25. <input type="text" name="autor" id="autor" value="">
  26. <input type="text" name="titulo" id="titulo" value="">
  27. <input type="submit" value="Buscar"><br> </form>
  28. <?
  29.  
  30.  
  31.  
  32.  
  33. $db = mysql_connect('localhost','root','')or die (mysql_error()); ;
  34.  
  35. $con = mysql_select_db('libros',$db)or die (mysql_error()); ;
  36.  
  37. $Campos = array ('tema','autor','titulo');
  38.  
  39. buscar('libros',$Campos,$db,$con);
  40.  while ($row = mysql_fetch_array($result)){
  41.  
  42. echo $row['autor'];
  43.  
  44.  }


esta es mi función que no va bien