Below is some code I wrote that queries a database to pull up product names from a database. The 'kwords' field in the database contains product names and product codes.
Here's an example record from the kwords field:
5000
Reducing Adapter
The product code '5000' has several products within it. For example, 5000-05, 5000-10, 5000-15. The problem lies when a user enters in 5000-05 and tries to search, it will come up with 'No products found'. But if the user enters in the code '5000' it will find all products.... I guess my question is if someone enters 5000-05, how do I go about stripping the '-05' from it so it will find the proper results?
<?
if ( !$searchterm )
{
echo "You have not entered search details. Please go back and try again.";
exit;
}
$searchterm = addslashes($searchterm);
@ $db = mysql_connect("localhost", "db1", "*****");
if (!$db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}
mysql_select_db("db1");
$input_array=explode(" ",$searchterm);
$cnt=count($input_array);
$stmt = "SELECT tnail, code, title FROM general WHERE kwords like '%".$input_array[0]."%'";
if ($cnt > 1)
{
for ($i=1;$i<=($cnt-1);$i++)
{
$stmt = $stmt." and kwords like '%".$input_array[$i]."%'";
}
}
$result = mysql_query($stmt);
$num_results = mysql_num_rows($result);
# Output statements, amout of items, string used
echo "<br><b>Number of products found: ".$num_results."</b>";
echo "<br>Search string used:<b><i> ".$searchterm."</i></b>";
$cnt = "1";
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
$a_tnail = stripslashes($row["tnail"]);
$a_code = stripslashes($row["code"]);
$a_title = stripslashes($row["title"]);
echo "<table width=97% border=1 cellspacing=0 cellpadding=1 bordercolor=ececec>";
echo "<tr bgcolor=#ececec></tr>";
echo "<tr><td width =100><div align=center><img src=http://www.********.com/html/products/$a_tnail></div></td>";
echo "<td width = 100><div align=center><font color=#333333 size=2 face=Verdana, Arial, Helvetica, sans-serif><a href=page.php?page=$a_code><b>$a_code</b></a></font></div></td>";
echo "<td><div align=left><font color=#333333 size=2 face=Verdana, Arial, Helvetica, sans-serif>$a_title</div></font></td></tr>";
echo "<br>";
echo "</table>";
echo " <!-- end of table -->";
}
?>
Thanks in advance!
- bobII