The only thing that doesn't work with my current search script is that when I search for two or more words, it will only bring up results when these words follow each other. I'd also like to bring up only results that include all search terms.

For example, if I search for "royal apple", and a field in my db has "royal gala apple", it won't be displayed.

I know it's because the query is looking for the string "royal apple", but can't find it... I read about the explode function, but I'm not sure how to apply it.

Here's my search script (simplified for this forum) :

<?php
echo '
<html>
<body>
<table>
<tr>
<td colspan=2><h2>Search</h2>
<form name="search" method="post" action="search.php">
Search for: <input type="text" name="find" />
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
</td>
</tr>
';
// We perform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo '
<tr>
<td colspan=2>
<h2>Results for ' . $find . '</h2><p>
</td></tr>';

//If they did not enter a search term we give them an error
if ($find == "")
{
echo '
<tr>
<td colspan=2>
You forgot to enter a search term
</td></tr>
</table>
</td>
</TR>';
exit;
}

// Otherwise we connect to our Database
include('db_login.php');
mysql_connect($db_host, $db_username, $db_password) or exit(mysql_error());
mysql_select_db($db_database) or exit(mysql_error());

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM mydatabase WHERE upper(description) LIKE '%$find%' OR upper(title) LIKE '%$find%' OR upper(partno) LIKE '%$find%' ORDER BY price,partno");
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo '
<tr height=30>
<td valign=center align=center colspan=4 class=cuetop>
<font color="#000000">' . $result['title'] . '</font>
</td>
</tr>
<tr height=30>
<td valign=center align=center colspan=4 class=cuetop>
<font color="#000000">' . $result['description'] . '</font>
</td>
</tr>

';
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{ echo '
<tr height=15><td colspan=2>
Sorry, but we can not find an entry to match your query
</td></tr>
';
}
echo '
</table>
</td>
</TR>
</body>
</html>
';
}
else {
echo'
</table>
</td>
</TR>
</body>
</html>';
};

?>

Thanks for your help.

    Write a Reply...