PHP Search Engine Help



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" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Search Engine</title>


<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' />

    $k = $_GET['k'];
	$terms = explode (" ", $k, 1);
	$query = "Select * FROM search WHERE ";
	foreach ($terms as $each) {
		if ($i == 1)
		   $query .= "keywords LIKE '%$each%' ";
			$query .= " OR keywords LIKE '%$each%' ";

// Run the actual connection here  
mysql_connect("host", "db_username", "password");

$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]
	echo "No resluts found for \"<b>$k</ b>\"";

// disconnect


Thanks in advance for your help


Well, search engines are complicated. First, you have to know your site and database inside and out.
I am sure we can help you, but, it is much easier once the rest is complete to add it in. I assume you
are just gathering ideas for your site.

For example, if you have one item to search such as Product-Name, that is easy and can be done with the simple code you currently have. But, if you have 400 products broken into 20 categories, then it becomes a bit more tricky. Also, previous searches by others, which brand is the hottest. And, you may want to push certain products more than others, so there are hit percentages and oldest-products first options… Lots of possible ways to search…

Now that I have you thinking hard about it all, a couple of comments on this sample you posted. It is a one page program. The HTML/FORM is on the same page as the PHP routine. So, this page can search and display it’s results and give a list of product links or whatever. But, it does not allow for a second search without backing up your browser.

In general, this would be done on two pages. The first would the the HTML/FORM page calling the second page that would the the PHP code creating the results. Why I am going into this so deeply is to explain that it would be easier when you have the products page completed first.

So, in your Products page, you would be pulling products based on some default that are displayed when the user goes to that page. This is done inside your query. Something like “SELECT * FROM products”.
So, which the search options in place you would pass the keywords that the user typed into the
search textfield as an argument to the Products page. Checking this way $search=$_POST[‘search’], where the ‘search’ would be the name of the search textfield. If the $search="" then, display the normal
default page. If the $search has data, change the query form the standard above to something like
“SELECT * FROM products WHERE keywords=’” . $search . “’”; And, this set up depends on how your
database is set up and what you are search thru.

Hope all of this makes sense. I would leave the search engine until you have your products page up and
working. Much easier after that is completed… Hope I gave you plenty of “food for thought”!!