E-Commerce Exercise 2
Sample E-commerce Search Page
<?php
session_start();
session_register("cart_items");
session_register("cart_quantities");
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
}
// Register Session
Variables
session_start();
session_register(array(cart_items));
session_register(array(cart_quantities));
if (isset($_GET[search_input]))
{
// 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>
<?
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
// 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
}
?>