Explode the kewords list. I have mine explode the keyword and search
the description field. It CAN limit it by category AND/OR by Vendor.
But this will do the trick without the limiters.
$keyword = $_POST[keyword];
$keywords = explode(" ", $keyword);
// so you can search for each word they enter
if ($category == "") {
$category = '%';
}
if ($vendor == "") {
$vendor = '%';
}
$query = "select * from DATABASE where vendor like '$vendor' AND
cat_code like '$category'";
// set up the part of the query that will always be the same
if (!$keywords) {
$keywords = '%';
//in case they don't enter any text
} else {
for ($i = 0; $i < count($keywords); $i++) {
$query .= " AND description like '%$keywords[$i]%'";
//modify the query to search for each word they enter
}
}
$query .= " order by item_num asc";
// to sort alphabetically
$result = mysql_query($query);
/* Determine the number of records returned */
while ($row = MYSQL_FETCH_ROW($result))
$number = mysql_numrows($result);
if (!$number)
{
print("There are no results that match your query<br>");
}
else
{
/* Print the relevant information */
$i = 0;
print "<table cellspacing=\"0\" cellpadding=\"5\"
border=\"1\">";
while ($i < $number)
{
$item_id = mysql_result($result, $i,"id");
$item_name = mysql_result($result, $i,"item_num");
$item_desc = mysql_result($result, $i,"description");
if ($i%2 == 0)
{
print "<tr
bgcolor=\"#eeeeee\"><td>What do you want done with the data</td><tr>\n";
}
else
{
print "<tr
bgcolor=\"#ffffff\"><td>What do you want done with the data</td><tr>\n";
}
$i++;
}
print "</table>";
}
/* Close the database connection */
MYSQL_CLOSE();
?>
</body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Love and you will be loved, and you will be able to do all that you
could not do unloved.
-Marques de Santillana.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----Original Message-----
From: Adam Williams [mailto:[EMAIL PROTECTED]
Sent: Wednesday, November 12, 2003 12:50 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: [PHP-DB] keyword searching
Hello,
I am selecting a field in a database called description for keyword
searching. The field contains names of people, states, years, etc.
When
someone searches for say "holmes north carolina" the query searches for
exactly that, fields which have "holmes north carolina", and not fields
that contaim holmes, north, and carolina. So I run a str_replace to
replace all spaces with *, which turns it into "holmes*north*carolina",
but say that north carolina is before holmes in the field, it won't
return
those fields (it only returns fields in which holmes is infront of north
carolina). So how can I have it return all fields which contain all
the words holmes, north, and carolina in any order, in that field?
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php