E-Commerce Exercise 2

Sample E-commerce Search Page

 

  1. The first thing we will need to start our e-commerce site is a search page to search through our database for items.  This can be accomplished easily with one .php page.
  2. In our e-commerce site, we will use sessions to keep track of the users shopping.  If a user closes their web browser, all information will be lost (whereas a cookie will store the information until the cookie expires).
  3. To begin, we must start EVERY page in our site with the start_session() function and register ALL of the session variables that we wish to store shopping cart information.  If the user comes to the search page, the information will be stored even though we won’t need to access the information in the session variables on the search page.

 

<?php

session_start();

session_register("cart_items");

session_register("cart_quantities");

 

  1. After we have registered the session and session variables, we can start to code our search page.  The basic outline of the page will go something like this:

 

If (isset($_GET[search_input]))

{

connect to database

search (SELECT * FROM…) database

$temp = # of results search found

if ($temp > 0)

{

return results in a table

           }

           else

           {

                      display “No results found”

           }

}

 

else

{

display HTML input form

}

 

  1. You can write the code for this page with knowledge from PHP Exercise 4.
  2. The first section of code from this page simply registers the session variables and includes the overall IF statement:

 

// Register Session Variables

session_start();

session_register(array(cart_items));

session_register(array(cart_quantities));

 

if (isset($_GET[search_input]))

{

 

  1. Next, the page connects to the database.  In the sample website, and IF/ELSE statement is used to determine whether the input form has been submitted.  The following section of code connects to the database and searches the database for the users input.  It is located inside the overall "IF" section of code.

 

// Connect to the database

           $dbcnx = mysql_connect("localhost", "bobby","bobby_passwd") or die("Could not connect to the database.");

           mysql_select_db("bobby",$dbcnx) or die("Can not select database");

          

           // Search the database

           // $_GET[field] is the input from the drop-down box

           // $_GET[search_input] is the text box input from the form

           // I won't display the item_number, but will use it if the user

           // decides to add the item to their shopping cart

           $query = "SELECT item_number,artist,cd_name,price,genre FROM cd_store WHERE $_GET[field]=\"$_GET[search_input]\" ";

           $query = stripslashes($query);

           $result = mysql_query($query) or die(mysql_error());

 

           // Return how many fields we selected with our *

           $number_cols = mysql_num_fields($result);

           $temp = mysql_num_rows($result);

 

           // Set up initial HTML headers

           ?>

           <HTML>

           <HEAD>

           <TITLE>Bobby's CD Search Results</TITLE>

           </HEAD>

           <BODY>

           <?

 

  1. Next, we will have an embedded IF/ELSE statement to either display the results of the search, or to advise the user that no results were found.  If there were results from the search ($temp is greater than 0), then we want to display the results to the user.  Again this is an embedded IF/ELSE statement under the overall "IF" statement.

 

    if ($temp > "0")

           {

                      // Set up the initial table and header rows

                      // Note that the \n character inserts a new line

                      // This just makes it easier to read the HTML source

                      // produced by this .php file

                      echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n

                      <tr><td>Artist</td><td>CD Name</td><td>Price</td><td>Genre</td><td>Add To Cart</td></tr>\n";

 

                      // While mysql_fetch_row returns a row (or a TRUE)

                      // Print out a new row in the table

                      while ($row = mysql_fetch_row($result))

                      {

                                echo "<tr>\n";

                                // Start from 1 because $row[0] is the item ID #. 

                                // I don’t want to display the item ID #

                                 for ($i=1; $i<$number_cols; $i++)

                                {

                                           echo "<td>";

                                           // If we are in the price column, put a "$" before

                                           // the data that comes from the database

                                           if ($i =="3")

                                           {         

                                                      echo "\$";

                                                      print_r($row[$i]);

                                           }

                                           else

                                           {

                                                      print_r($row[$i]);

                                           }

                                           echo "</td>";

                                }

                                // Display the Add To Cart button in the last

                                // cell of every row

                                echo "<td><form action=\"cart.php\" method=\"GET\">

                                <input type=\"hidden\" name=\"action\" value=\"add_to_cart\">

                                <input type=\"hidden\" name=\"item_number\" value=\"$row[0]\">

                                <input type=\"submit\" value=\"Add To Cart\"></form></td>";

                                echo "</tr>\n";

                                }

                      echo "</BODY></HTML>\n";

           }  // end's the if($temp >0) statement

          

           else  // No results were found - display corresponding message.

           {

                      echo "Your search returned 0 results.  Please try <a href=\"search.php\">again</a>";

           }

} //ends the if(isset$_GET[search_input]) statement

 

  1. Now we are done with the overall IF statement.  The following code is the ELSE part of the overall IF/ELSE statement:

 

// if the form hasn't been submitted, then display

// this HTML form to get the input from the user

else 

{

// END PHP Code

?>

<HTML>

<HEAD>

<TITLE>Bobby's CD Search Page</TITLE>

</HEAD>

<BODY>

<H1>Bobby's CD Search Page</H1>

<BR>

// Go back into PHP so we can call ourself

<?php

echo "<FORM action=\"$_SERVER[PHP_SELF]\" method=\"GET\">";

?>

<TABLE>

<TR><TD>What Field To Search:</TD><TD><SELECT name="field">

<option value="artist">Artist</option>

<option value="cd_name">CD Name</option>

<option value="genre">Genre</option>

</select></TD></TR>

<TR><TD>Input:</TD><TD><input type="Text" name="search_input"></TD></TR>

<TR><TD COLSPAN="2"><center><input type="submit" value="Submit"></center></TD></TR></TABLE>

</FORM>

</BODY>

</HTML>

 

<?

// End the "else" statement

}

?>

 

  1. The actual php code that corresponds to the pseudocode from step 4 can be found here.  Pay special attention to the comments to see what each section of code does.  In addition, some instructions are found at the very top.
  2. Notice that this search will only find a match if you enter in the artist, CD name, or genre EXACTLY as it appears in the database.