Hello everyone, first post here tried my first attempt to create a search form, for a friend of mine but encountered some problems, hoping to get some suggestions from here
It works just fine, but the problem is that if I look for a specific search, lets say: “Spitfire RAF”, the results I get are all items with “Spitfire RAF” but also all items with the “RAF” or “Spitfire” included, which are not needed. I’d like to find a way on how to specify this please
here is the code in the search.php page results:
[php]$term = mysql_real_escape_string($_GET[‘term’]);
$exploded=explode(" ",$term);
$query="select * from models where ";
foreach($exploded as $term)
{
$query.=" Brand like ‘%".$term."’ OR ";
}
$query.=" AND ";
foreach($exploded as $term)
{
$query.=" Modelref like ‘%".$term."’ OR ";
}
$query.=" AND ";
foreach($exploded as $term)
{
$query.=" Scale like ‘%".$term."’ OR ";
}
$query.=" AND ";
foreach($exploded as $term)
{
$query.=" Kind like ‘%".$term."’ OR ";
}
$query.=" AND ";
foreach($exploded as $term)
{
$query.=" Type like ‘%".$term."’ OR ";
}
$query.=" AND ";
foreach($exploded as $term)
{
$query.=" Category like ‘%".$term."’ OR ";
}
$query.=" AND ";
foreach($exploded as $term)
{
$query.=" Description like ‘%".$term."’ OR ";
}
$query="select * from models where Brand like ‘%$term%’ or Modelref like ‘%$term%’ or Scale like ‘%$term%’ or Kind like ‘%$term%’ or Type like ‘%$term%’ or Category like ‘%$term%’ or Description like ‘%$term%’ ";
$sql = mysql_query($query);
echo mysql_error();
if(mysql_num_rows($sql) == 0)
{
echo “Sorry but your search hasn’t brought any results.”;
}
else
{
while ($row = mysql_fetch_array($sql))
{
echo '<center>';
echo '<table width=850 bgcolor=#FFFFFF style=font-family:Arial, Helvetica, sans-serif class=db_text border=0 cellspacing=10><tr><td width=400 rowspan=13>';
echo "<center><img src='".$row['imagebrand']."'>";
echo "<br><img src='".$row['imageproduct']."'></center>";
echo '</td>';
echo '</tr><tr>';
echo '<td bgcolor=#dadada width=200><strong> Status:</strong></td>';
echo '<td bgcolor=#dadada width=250>';
echo ''.$row['Status'];
echo '</td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#daf3d7 width=200><strong> Brand:</strong></td>';
echo '<td bgcolor=#daf3d7 width=250>';
echo ''.$row['Brand'];
echo '</td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#dadada><strong> Scale:</strong></td>';
echo '<td bgcolor=#dadada width=250>';
echo ''.$row['Scale'];
echo '</td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#daf3d7 width=200><strong> Kind:</strong></td>';
echo '<td bgcolor=#daf3d7 width=250>';
echo ''.$row['Kind'];
echo '</td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#dadada><strong> Type:</strong></td>';
echo '<td bgcolor=#dadada width=250>';
echo ''.$row['Type'];
echo '</td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#daf3d7 width=200><strong> Category:</strong></td>';
echo '<td bgcolor=#daf3d7 width=250>';
echo ''.$row['Category'];
echo '</td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#dadada><strong> Description:</strong></td>';
echo '<td bgcolor=#dadada width=250>';
echo ''.$row['Description'];
echo '</td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#daf3d7 width=200><strong> Quantity:</strong></td>';
echo '<td bgcolor=#daf3d7 width=250>';
echo ''.$row['Quantity'];
echo '</td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#dadada width=200><strong> Location:</strong></td>';
echo '<td bgcolor=#dadada width=250>';
echo ''.$row['Location'];
echo '</td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#daf3d7 width=200><strong> RRP:</strong></td>';
echo '<td bgcolor=#daf3d7 width=250 style=color:#990000><strong>';
echo '€'.$row['RRP'];
echo '</strong></td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#dadada width=200><strong> RRPreduced:</strong></td>';
echo '<td bgcolor=#dadada width=250 style=color:#990000><strong>';
echo '€'.$row['RRPreduced'];
echo '</strong></td><td>';
echo '</tr><tr>';
echo '<td bgcolor=#daf3d7 width=200><strong> Arrives:</strong></td>';
echo '<td bgcolor=#daf3d7 width=250>';
echo ''.$row['Arrives'];
echo '</td><td>';
echo '</td></tr></table>';
echo '<br>';
echo '</center>';
}
}
mysql_free_result($sql);
?>[/php]
and this is the form I’m using in the home page:
[php]
[/php]
Any help would be greatly appreciated