Foros del Web » Programando para Internet » PHP »

Buscador en varios campos

Estas en el tema de Buscador en varios campos en el foro de PHP en Foros del Web. Hola a todos. Se me ha presentado un problema y realmente no se como hacerlo. Tengo que hacer un buscador que me busque en en ...
  #1 (permalink)  
Antiguo 12/05/2005, 05:44
 
Fecha de Ingreso: noviembre-2002
Mensajes: 130
Antigüedad: 21 años, 5 meses
Puntos: 0
Buscador en varios campos

Hola a todos.

Se me ha presentado un problema y realmente no se como hacerlo. Tengo que hacer un buscador que me busque en en cinco campos distintos de la misma tabla.

El problema que se me presenta es que no se como mostrar los resultados.

Les envio el codigo para que vean y me comenten algo si le ven la solución.

Un saludo

Código PHP:
<? include("../includes/config.php"); ?>
<?php
$palabra 
$HTTP_POST_VARS['palabra'];
if (isset(
$HTTP_POST_VARS['enviar'])) { 
$resp mysql_query("select id, ficheroarticulo, articulo1texto, articulo2texto, articulo3texto, articulo4texto, articulo5texto from revista where articulo1texto LIKE '%".$HTTP_POST_VARS[palabra]."%' or articulo2texto LIKE '%".$HTTP_POST_VARS[palabra]."%' or articulo3texto LIKE '%".$HTTP_POST_VARS[palabra]."%' or articulo4texto LIKE '%".$HTTP_POST_VARS[palabra]."%' or articulo5texto  LIKE '%".$HTTP_POST_VARS[palabra]."%'") or die (mysql_error());
if(
mysql_num_rows($resp) == "0") {
echo 
"No hay resultados";
} else {
?>

<link href="../style/miestilo.css" rel="stylesheet" type="text/css">
<table width="100%" border="0" cellpadding="0" cellspacing="3">
  <tr>
    <td class="botonform"><div align="center" class="negrita">La Revista </div></td>
  </tr>
</table>
<form action="buscador.php" method="post">
<table width="100%" height="20" border="0" align="center" cellpadding="0" cellspacing="3">
  <tr>
      <td class="footer" >
        <div align="right"> Buscador de art&iacute;culos de la Revista
            <input name="palabra" type="text" class="input_text" size="60">
            <input name="enviar" type="submit" class="inputform" value="Buscar">
      </div></td>
  </tr>
</table>
</form>
<table width="100%" height="75%"  border="0" cellpadding="1" cellspacing="5" class="marco">
  <tr class="fonsblau_claret">
    <td valign="top"><? echo "Resultados de la busqueda:<br>"?><br><br>
    <table width="100%"   border="0" cellspacing="3" cellpadding="0" class="marco">
    <? 
    
while ($buscador=mysql_fetch_object($resp)){
    
    if (
$palabra == $buscador['articulo1texto']) { ?>
    <tr>
  <td valign="top"><div align="justify"><a href="../larevista/<?php echo $buscador->ficheroarticulo;?>"><? echo $buscador->articulo1texto;?></a> </div></td>
  </tr>
  <? } elseif ($palabra == $buscador['articulo2texto']) {?>
  <tr>
  <td valign="top"><div align="justify"><a href="ver_detalle.php"><? echo $buscador->articulo2texto;?></a> </div></td>
  </tr>
  <? } elseif ($palabra == $buscador['articulo3texto'])  {?>
  <tr>
  <td valign="top"><div align="justify"><a href="ver_detalle.php"><? echo $buscador->articulo3texto;?></a> </div></td>
  </tr>
  <? } elseif ($palabra == $buscador['articulo4texto'])  {?>
  <tr>
  <td valign="top"><div align="justify"><a href="ver_detalle.php"><? echo $buscador->articulo4texto;?></a> </div></td>
  </tr>
  <? } elseif ($palabra == $buscador['articulo5texto'])  {?>
  <tr>
  <td valign="top"><div align="justify"><a href="ver_detalle.php"><? echo $buscador->articulo5texto;?></a> </div></td>
  </tr><? }  }?>
    </table></td>
  </tr>
</table>
<?
}
}
?>

Última edición por Faser; 12/05/2005 a las 14:58
  #2 (permalink)  
Antiguo 12/05/2005, 06:00
Avatar de Seppo  
Fecha de Ingreso: marzo-2005
Ubicación: Buenos Aires, Argentina
Mensajes: 1.284
Antigüedad: 19 años, 1 mes
Puntos: 17
¿Cual es el problema que tienes con ese código? Supongo que te está devolviendo vacio...

A ver lo siguiente...
Código PHP:
<? include("../includes/config.php"); ?>
<?php
$palabra 
$HTTP_POST_VARS['palabra'];
if (isset(
$HTTP_POST_VARS['enviar'])) {
$resp mysql_query("select id, ficheroarticulo, articulo1texto, articulo2texto, articulo3texto, articulo4texto, articulo5texto from aedn_revista where articulo1texto LIKE '%".$palabra."%' or articulo2texto LIKE '%".$palabra."%' or articulo3texto LIKE '%".$palabra."%' or articulo4texto LIKE '%".$palabra."%' or articulo5texto  LIKE '%".$palabra."%'") or die (mysql_error());

if(
mysql_num_rows($resp) == 0) {
  echo 
"No hay resultados";
} else {
?>
<link href="../style/miestilo.css" rel="stylesheet" type="text/css">
<table width="100%" border="0" cellpadding="0" cellspacing="3">
  <tr>
    <td class="botonform"><div align="center" class="negrita">La Revista </div></td>
  </tr>
</table>
<form action="buscador.php" method="post">
<table width="100%" height="20" border="0" align="center" cellpadding="0" cellspacing="3">
  <tr>
      <td class="footer" >
        <div align="right"> Buscador de art&iacute;culos de la Revista
            <input name="palabra" type="text" class="input_text" size="60">
            <input name="enviar" type="submit" class="inputform" value="Buscar">
      </div></td>
  </tr>
</table>
</form>
<table width="100%" height="75%"  border="0" cellpadding="1" cellspacing="5" class="marco">
  <tr class="fonsblau_claret">
    <td valign="top"><? echo "Resultados de la busqueda:<br>"?><br><br>
    <table width="100%"   border="0" cellspacing="3" cellpadding="0" class="marco">
    <?
    
while ($buscador=mysql_fetch_object($resp)){
    
?><tr>
      <td valign="top"><div align="justify"><?    
      
if (strstr($buscador['articulo1texto'],$palabra)) { ?>
<a href="../larevista/<?php echo $buscador->ficheroarticulo;?>"><? echo $buscador->articulo1texto;?></a>
      <? } elseif (strstr($buscador['articulo2texto'],$palabra)) { ?>
      <a href="ver_detalle.php"><? echo $buscador->articulo2texto;?></a>
      <? } elseif (strstr($buscador['articulo3texto'],$palabra)) { ?>
      <a href="ver_detalle.php"><? echo $buscador->articulo3texto;?></a> 
      <? } elseif (strstr($buscador['articulo4texto'],$palabra)) { ?>
      <a href="ver_detalle.php"><? echo $buscador->articulo4texto;?></a> 
      <? } elseif (strstr($buscador['articulo5texto'],$palabra)) { ?>
      <a href="ver_detalle.php"><? echo $buscador->articulo5texto;?></a> 
    <? }  ?>
    </div></td>
    </tr>
  }?>
    </table></td>
  </tr>
</table>
<?
}
}
?>
  #3 (permalink)  
Antiguo 12/05/2005, 14:53
 
Fecha de Ingreso: noviembre-2002
Mensajes: 130
Antigüedad: 21 años, 5 meses
Puntos: 0
Perfecto, logré solucionarlo.
Como bien dices me llegaba vacio, sin nada.

Aunque me miraré detenidamente tu codigo, ya que me parece que lo has reducido más de lo que yo he echo.

De todas formas muchas gracias
  #4 (permalink)  
Antiguo 12/05/2005, 14:57
 
Fecha de Ingreso: noviembre-2002
Mensajes: 130
Antigüedad: 21 años, 5 meses
Puntos: 0
Por si alguien quiere el código ahí va

Código PHP:
<? include("../includes/config.php"); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../style/miestilo.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.Estilo1 {color: #FFFFFF}
-->
</style>
</head>
<body>
<?php
$palabra 
$HTTP_POST_VARS['palabra'];
if (isset(
$HTTP_POST_VARS['enviar'])) { 
$resp mysql_query("select id, numero, ficheroarticulo, articulo1texto, articulo2texto, articulo3texto, articulo4texto, articulo5texto from revista where articulo1texto LIKE '%".$HTTP_POST_VARS[palabra]."%' or articulo2texto LIKE '%".$HTTP_POST_VARS[palabra]."%' or articulo3texto LIKE '%".$HTTP_POST_VARS[palabra]."%' or articulo4texto LIKE '%".$HTTP_POST_VARS[palabra]."%' or articulo5texto  LIKE '%".$HTTP_POST_VARS[palabra]."%' order by numero asc") or die (mysql_error());
if(
mysql_num_rows($resp) == "0") { ?>
<table width="100%"  border="1" align="center" cellpadding="5" cellspacing="2" bordercolor="#FFFFFF" class="fonsverd_claret">
  <tr>
    <td><div align="center" class="Estilo1"><strong> <img src="../images/alert.red.gif" width="20" height="22" align="absmiddle"> NO SE HAN ENCONTRADO RESULTADOS EN LA B&Uacute;SQUEDA</strong></div></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF"><div align="center"><a href="javascript:history.back()">VOLVER A INTENTARLO </a></div></td>
  </tr>
</table>
<?
//echo "No hay resultados";
} else {
?>
<table width="100%" border="0" cellpadding="0" cellspacing="3">
  <tr>
    <td class="botonform"><div align="center" class="negrita">La Revista </div></td>
  </tr>
</table>
<form action="buscador.php" method="post">
<table width="100%" height="20" border="0" align="center" cellpadding="0" cellspacing="3">
  <tr>
      <td class="footer" >
        <div align="right"> Buscador de art&iacute;culos: 
          <input name="palabra" type="text" class="input_text" size="60">
            <input name="enviar" type="submit" class="inputform" value="Buscar">
      </div></td>
  </tr>
</table>

</form>
<table width="100%" height="75%"  border="0" cellpadding="1" cellspacing="5" class="marco">
  <tr class="fonsblau_claret">
    <td valign="top"><? echo "Resultados de la busqueda:<br>"?><br><br>
    <table width="100%"   border="0" cellspacing="3" cellpadding="0">
    <? 
    
while ($buscador=mysql_fetch_object($resp)){
    if (
$palabra=="") {
    
?>
    <?
    $variable 
$buscador->articulo4texto;
    if (!empty(
$variable)) 
    { 
?>
    <tr>
          <td width="25" valign="top"><div align="justify"><img src="../images/catalogopdf.gif" width="32" height="31" align="absmiddle"></div></td>
          <td valign="middle"> <span class="news">(<? echo $buscador->numero;?>)</span> <a href="../larevista/<?php echo $buscador->ficheroarticulo;?>"></a><a href="../larevista/<?php echo $buscador->ficheroarticulo;?>"> <? echo $buscador->articulo1texto;?></a> </td>
    </tr>
    <?
    
}
    else
    { 
    
//echo "<img src='../images/empty.gif' width='1' height='1' border='0'>";
    
}
    
?>
    <?
    $variable 
$buscador->articulo4texto;
    if (!empty(
$variable)) 
    { 
?>
    <tr>
          <td valign="top"><div align="justify"><img src="../images/no_busca.gif" align="absmiddle"><span class="news"> </span></div></td>
          <td valign="middle"><span class="news">(<? echo $buscador->numero;?>)</span> <a href="ver_detalle.php"><? echo $buscador->articulo2texto;?></a> </td>
    </tr>
    <?
    
}
    else
    { 
    
//echo "<img src='../images/empty.gif' width='1' height='1' border='0'>";
    
}
    
?>
    <?
    $variable 
$buscador->articulo4texto;
    if (!empty(
$variable)) 
    { 
?>
    <tr>
          <td valign="top"><div align="justify"><img src="../images/no_busca.gif" align="absmiddle"> </div></td>
          <td valign="middle"><span class="news">(<? echo $buscador->numero;?>)</span> <a href="ver_detalle.php"> <? echo $buscador->articulo3texto;?></a> </td>
    </tr>
    <?
    
}
    else
    { 
    
//echo "<img src='../images/empty.gif' width='1' height='1' border='0'>";
    
}
    
?>
    <?
    $variable 
$buscador->articulo4texto;
    if (!empty(
$variable)) 
    { 
?>
       <tr>
          <td valign="top"><div align="justify"><img src="../images/no_busca.gif" align="absmiddle">  </div></td>
          <td valign="middle"><span class="news">(<? echo $buscador->numero;?>)</span> <a href="ver_detalle.php"> <? echo $buscador->articulo4texto;?></a> </td>
       </tr>
    <?
    
} else { 
    
//echo "<img src='../images/empty.gif' width='1' height='1' border='0'>";
    
}
    
?>
    <?
    $variable 
$buscador->articulo5texto;
    if (!empty(
$variable)) 
    { 
?>
      <tr>
          <td valign="top"><div align="justify"><img src="../images/no_busca.gif" align="absmiddle"> </div></td>
          <td valign="middle"><span class="news">(<? echo $buscador->numero;?>)</span> <a href="ver_detalle.php"> <? echo $buscador->articulo5texto;?></a> </td>
      </tr>
    <?
    
}  else    { 
    
//echo "<img src='../images/empty.gif' width='1' height='1' border='0'>";
    
}
    
?>  
    <?
    
} else {
    if (
ereg($palabra,$buscador->articulo1texto)) {    
    
?>
    <tr>
          <td valign="top"><div align="justify"><img src="../images/catalogopdf.gif" width="32" height="31" align="absmiddle"></div></td>
          <td valign="middle"><span class="news">(<? echo $buscador->numero;?>)</span> <a href="../larevista/<?php echo $buscador->ficheroarticulo;?>"> <? echo $buscador->articulo1texto;?></a> </td>
    </tr>
      <? 
      if (
ereg($palabra,$buscador->articulo2texto)) {    
      
?>
      <tr>
          <td valign="top"><div align="justify"><img src="../images/no_busca.gif" align="absmiddle"> </div></td>
          <td valign="middle"><span class="news">(<? echo $buscador->numero;?>)</span> <a href="ver_detalle.php"></a><a href="ver_detalle.php"> <? echo $buscador->articulo2texto;?></a> </td>
      </tr>
      <? 
      
}
      if (
ereg($palabra,$buscador->articulo3texto)) {
      
?>
      <tr>
          <td valign="top"><div align="justify"><img src="../images/no_busca.gif" align="absmiddle"><span class="news"> </span></div></td>
          <td valign="middle"><span class="news">(<? echo $buscador->numero;?>)</span> <a href="ver_detalle.php"><? echo $buscador->articulo3texto;?></a> </td>
      </tr>
      <? 
      
}
      if (
ereg($palabra,$buscador->articulo4texto)) {    
      
?>
      <tr>
          <td valign="top"><div align="justify"><img src="../images/no_busca.gif" align="absmiddle"></div></td>
          <td valign="middle"> <span class="news">(<? echo $buscador->numero;?>)</span> <a href="ver_detalle.php"> <? echo $buscador->articulo4texto;?></a> </td>
      </tr>
      <? 
      
}
      if (
ereg($palabra,$buscador->articulo5texto)) {    
      
?>
      <tr>
          <td valign="top"><div align="justify"><img src="../images/no_busca.gif" align="absmiddle"></div></td>
          <td valign="middle"> <span class="news">(<? echo $buscador->numero;?>)</span> <a href="ver_detalle.php"> <? echo $buscador->articulo5texto;?></a> </td>
      </tr>
      <? } } } ?>
    </table>
  </td>
  </tr>
</table>
<? } } ?>
</body>
</html>
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 02:43.