Hello,
I am a just starting to learn php + mysql. I am currently trying to setup my ecommerce website and wanted to add a search function to my website that would grab products based on my customers keywords. I found a script to use but I don’t like the fact that it shows results underneath the search function before any keywords actually go into the search text box.
I want to be able to have the echo results displayed after the keywords have been submitted, like a normal search function. I can’t seem to get it.
Here is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Search Engine</title>
</head>
<body>
<h2>Search Engine</h2>
<form action='./search.php' method='get' target="_new">
<input type='text' name='k' size='50' value='<?php echo $_GET['k']; ?>' />
<input type='submit' value='search' />
</form>
[php]
<?php
$k = $_GET['k'];
$terms = explode (" ", $k, 1);
$query = "Select * FROM search WHERE ";
foreach ($terms as $each) {
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= " OR keywords LIKE '%$each%' ";
}
// Run the actual connection here
mysql_connect("host", "db_username", "password");
mysql_select_db("db_database");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows >= 1) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row ['id'];
$title = $row ['title'];
$description = $row ['description'];
$keywords = $row ['keywords'];
$link = $row ['link'];
[b]echo "<h2><a href='$link'>$title</a></h2>
$description<br />";[/color]
[/b]
}
}
else
echo "No resluts found for \"<b>$k</ b>\"";
// disconnect
mysql_close();
?>
[/php
</body>
</html>
Thanks in advance for your help