Searching the Products

 

Introduction.  We want to build in some aspects of being able to search the site.  We will focus on searching products by looking for something in their name.  Thus, if someone is looking for something native or a DVD or a t-shirt or edible undies or anything like these, we want the shopper to be able to list out certain products with links.

So this page will need a number of characteristics

  • a form to take user inputs
  • we will make the form post to itself so that it can be used repeatedly
  • we want to search products
    • this could be fairly easily extended to departments
  • we want the SQL query to search for substrings of the product name independent of whether the letters are upper or lower case
    • we will make use of the LIKE operator and what are likely some other unfamiliar techniques
    • we could easily extend this to other characteristics of the products such as description and/or price
  • we want each product to automatically provide a link to a product page as we've already developed
    • we will display the product name and its description, with the name acting as the link

Now you need to copy the following code and name it search.php.  After the code we give a brief discussion of its overall structure and content.

 

<HTML>
<!-- search.php - processes and displays the results of a search -->
<head><title>The Firefox Search Page</title></head>
<?php
include("includes/header.php");
include("includes/connection_config.php");
?>
<br><br>
<form method="post" action="search.php">
<table align=center border=0 width=600 cellpadding=7>
<tr>
<td align="right" width="250"><font size="4" color="#c0c0c0">Search:</font>
</td>
<td align="left"><input type="text" width="20" style="font size: 14pt" name="txt_search" value="<?php echo $txt_search; ?>">
</td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="Search" style="font size: 14pt">
</td>
</tr>
</table>
</form>
<br>
<table align = "center" border = 0 width = 600 cellpadding=5>

<?php
if ($txt_search != "")
{

// assign the table for database access
$table_name = "products";

// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
// constructing the query string
$query_string = "SELECT * FROM $table_name WHERE product_name LIKE '%$txt_search%'";
$result_set = mysql_db_query($db_name, $query_string, $link);

// looping through the resultset appending a new row for each record
while ($row = mysql_fetch_array($result_set))
{

?>
<tr>
<td align=right valign=top width=250><font size=4 color="c0c0c0"> <a href="product.php?id_prod=<?php echo $row[id_product]; ?>"><?php echo $row[product_name]; ?> </a></font></td>
<td align=left valign=top><font size=4 color="c0c0c0"> <?php echo $row[product_description]; ?></font></td>
</tr>

<?php
}
mysql_close($link);

}
?>

</table>

</BODY>
</HTML>

 
As usual you also want to make sure you distinguish our PHP scriptlets from the HTML.  We move back and forth quite a bit.
  • We start by including the header.php. 
  • Notice how HTML is used to give a form for the user to enter the text they would like to search for.
    • Notice that it posts to itself.
  • Then our next set of code executes only if the user has entered some search text (this also prevents the query from occurring when the page is first loaded).
    • Next we establish all the characteristics required to connect to our database.
    • We create the query string and execute it to create our resultset.
      • notice we use a LIKE operator in our WHERE clause
      • notice how the $txt_search is enclosed in a '% and another %' so that it will find all product_name that contain the search string at any place within the name.
    • We then loop through this resultset while it has more entries appending a new row to the table for each such product.
      • within this loop we
      • create links to product.php that will display
        • product_name
        • product_description
      • but the actual product will be identified by using the id_prod that is passed in the URL.
        • this id_prod is obtained from the departments table using the id_product field
      • notice how the path to the images is constructed
    • Then we close the link to the database.

More will be said about this after you upload this and get it to work.  Passing extra arguments in the URL is making use of a GET approach rather than the POST we have used so far to pass information to a subsequent PHP.

We have no longer colored certain tricks since you should be getting more used to them.  Though, remember it can still be useful to reorganize the code after you copy it to assist your understanding.