Working with the Online Shopping Cart Application


The Client section contains the Web pages that enables the end user as clients to perform activities, such as buying a book by adding it to the shopping cart, and performing the search operation based on author name or book title. All the pages in the Client section of the application are divided into three sections: top, right, and bottom. The top and bottom sections are same for all the Web pages of the Client section. The right section differs for each Web page.

The first Web page of the client section is represented by the welcome.php file. The following code shows the content of the welcome.php file:

 <?php echo "Welcome"; include "tophtml.php"; include "right.php"; include "bottomhtml.php"; ?> 

The above code shows that the welcome.php file includes three other files: tophtml.php, right.php, and bottomhtml.php.

Listing 9-22 shows the content of the tophtml.php file:

Listing 9-22: The tophtml.php File
start example
 <?php $linkcol="#FF0000"; $linkcol1="#0000FF"; $bg="#7194D5"; $bg1="#4E6CA7"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Book Shop</title> <style type="text/css"> <!-- a {font: 10pt Arial, Verdana, Helvetica, sans-serif; text-decoration: none} p {font: 12pt Arial, Helvetica, sans serif} UL {font: 12pt Arial, Helvetica, sans serif} --> </style> <script> function usercheck() {    var user_id = document.newusr.user_id.value.replace(/\s+/,"");    var passwd = document.newusr.passwd.value.replace(/\s+/,"");    var cpasswd = document.newusr.cpasswd.value.replace(/\s+/,"");    var name = document.newusr.name.value.replace(/\s+/,"");    var address_line1 = document.newusr.address_line1.value.replace(/\s+/,"");    //var address_line2 = document.newusr.address_line2.value.replace(/\s+/,"");    var city = document.newusr.city.value.replace(/\s+/,"");    var country = document.newusr.country.value.replace(/\s+/,"");    var pin = document.newusr.pin.value.replace(/\s+/,"");    var email_id = document.newusr.email_id.value.replace(/\s+/,"");    var phone_number = document.newusr.phone_number.value.replace(/\s+/,"");    var fax_number = document.newusr.fax_number.value.replace(/\s+/,"");    var card_no = document.newusr.card_no.value.replace(/\s+/,"");    var expiry_date = document.newusr.expiry_date.value.replace(/\s+/,"");    var card_type = document.newusr.card_type.value.replace(/\s+/,"");    var error="no";    var message;    var focus;    if ((!user_id) && (error == "no"))    {       message = "Please enter User ID";       focus="user_id";       error="yes";    }    if ((!passwd) && (error == "no"))    {       message = "Please enter  Password";       focus="passwd";       error="yes";    }    if ((!cpasswd) && (error == "no"))    {       message = "Please enter Confirm Password";       focus="cpasswd";       error="yes";    }    if ((!name) && (error == "no"))    {       message = "Please enter  Name";       focus="name";       error="yes";    }    if ((!address_line1) && (error == "no"))    {       message = "Please enter  Address";       focus="address_line1";       error="yes";    }    if ((!city) && (error == "no"))    {       message = "Please enter  City";       focus="city";       error="yes";    }    if ((!country) && (error == "no"))    {       message = "Please enter  Country";       focus="country";       error="yes";    }    //PinCode Validation    if ((!pin) && (error == "no"))    {       message = "Please enter  Pin Code";       focus="pin";       error="yes";    }    else    {    var valid ="0123456789";    for (var i=0; i < pin.length; i++)     {       var temp = "" + pin.substring(i, i+1);       if (valid.indexOf(temp) == "-1")        {          alert("Invalid characters in your pin code");          focus="pin";          error="yes";          return false;       }    }    if ((pin.length < 6) && (error == "no"))    {       alert("Invalid your pin code");       focus="pin";       error="yes";    } } //validate phone number if ((!phone_number) && (error == "no")) {    message = "Please enter  Phone Number";    focus="phone_number";    error="yes"; } else {    var validph ="0123456789";    for (var i=0; i < phone_number.length; i++)    {       var temp = "" + phone_number.substring(i, i+1);       if (validph.indexOf(temp) == "-1")       {          alert("Invalid characters in your phone number.");          focus="phone_number";          error="yes";          return false;       }    }    if ((phone_number.length < 6) && (error == "no"))    {       alert("Invalid your phone number.");       focus="phone_number";       error="yes";    } } if ((!email_id) && (error == "no")) {    message = "Please enter  Email ID";    focus="email_id";    error="yes"; } //Card Number Validation if ((!card_no) && (error == "no")) {    message = "Please enter  Card Number";    focus="card_no";    error="yes"; } else {    var validcard ="0123456789";    for (var i=0; i < card_no.length; i++)    {       var temp = "" + card_no.substring(i, i+1);       if (validcard.indexOf(temp) == "-1")        {          alert("Invalid characters in your card number.");          focus="card_no";          error="yes";          return false;       }    }    if ((card_no.length < 6) && (error == "no"))    {       alert("Invalid your card number.");       focus="card_no";       error="yes";    } } if ((passwd != cpasswd) && (error == "no")) {    message = "Check Confirm Password";    focus="cpasswd";    error="yes"; } if (error == "yes") {    alert(message);    var str ="document.newusr."+focus+".focus()";    eval(str); } else {    document.newusr.submit(); } </script> <script language="JavaScript"> function MM_preloadImages()  {    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} } </script> </HEAD> <BODY BGCOLOR="#FFFFFF" onLoad="MM_preloadImages('/images/blank.gif')" > <center> <!-- begin table1--> <TABLE BORDER="0" WIDTH="700" CELLPADDING="0" CELLSPACING="0" BGCOLOR="#FFFFFF" > <tr> <td> <!-- begin table2--> <table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="600" style="margin: 0px; padding: 0px"> <!-- <tr> <td align="center"> <img src="/images/consbar.gif"> </td> </tr> --> <tr> <td HEIGHT="1" width="600" align="left" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1></td> </tr> <tr> <td HEIGHT="1" width="600" align="left"> <table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="600"> <tr > <td align="center" bgcolor="<?php if ($link == 1) echo $bg1; else echo $bg; ?>"> <A href="index.php?link=1"><font size="3" face="arial"  color="#FFFFFF"><b>&nbsp;BOOK SHOP</b></font></A> </td> </tr> </table> </td> </tr> <tr> <td HEIGHT="1" BGCOLOR="#1E237B" width="600" align="left" background="/images/line.gif"> <img SRC="/images/line.gif" ALT="-" BORDER=0 height=1 width=1></td> </tr> </table> <!--end table2--> </td> </tr> 
end example
 

The above listing creates the top section of the home page of the online shopping cart application.

The middle section displays the options, such as searching a book, and listing the books from a category.

Listing 9-23 shows the content of the right.php file to create the middle section of the home page:

Listing 9-23: Creating the Middle Section of the Home Page
start example
 <td BGCOLOR="#FF0000" width="1" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1> </td> <td width="150" valign="top"> <table width="150" border="1" cellspacing="2" cellpadding="2"> <tr> <td> <table width="150" border="1" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#7272B1" align="center"> <?php $login=$_GET['login']; if (($login == "ok")  ($sesid)) echo "<a href=\"../logout.php\"><font style=\"font-size:11px;\" face=\"Helvetica\" color=\"#33CC66\"><B>LOGOUT</B>"; else echo "<a href=\"new.php\"><font style=\"font-size:11px;\" face=\"Helvetica\" color=\"#FF0000\"><B>NEW USER</b></font></a><a href=\"../index.php\"><font style=\"font-size:11x;\" face=\"Helvetica\" color=\"#ffffff\"><B>LOGIN</B>"; ?> &nbsp;</td> </tr> </table> </td> </tr> <tr> <td> <table width="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#7272B1" align="center"> <form name="searchform" action="book_desc.php" method="GET"><BR> <input type="hidden" name="req_from" value="search"> <input type="text" name="search_word" size="10"><BR> <font style="font-size:12px;" face="Helvetica" color="#ffffff"><B>Search by</B></font><BR> <select name="search_by"> <option value="author">By Author</option> <option value="title">By Title</option> </select><BR><BR> <input type="submit" value="Search"> </form> </td> </tr> </table> </td> </tr> <tr> <td> <table width="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#7272B1" align="center"> <font style="font-size:12px;" face="Helvetica" color="#ffffff"><B>Select Book Category</B></font></td> </tr> </table> </td> </tr> <?php $db=mysql_connect('localhost','root',''); if (!$db) {    echo "Error When connecting to Database"; } mysql_select_db("shop", $db); $result_cat = mysql_query("select item_type from category", $db); print "<tr><td align=\"center\"><form name=\"form1\" method=\"get\" action=\"book_desc.php\"><table width=\"110\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\"><tr><td align=\"center\"><SELECT NAME=\"item_type\">"; while ($row_cat=mysql_fetch_array($result_cat)) {    print "<option value=\"$row_cat[0]\">$row_cat[0]</option>";    //print "<tr><td bgcolor=\"#FF3300\" align=\"center\"><a    href='book_desc.php?item_type=$row_cat[0]'><font style=\"font-size:12px;\"    face=\"Helvetica\"    color=\"#ffffff\">$row_cat[0]</font></a></td></tr>"; } print "</SELECT></td></tr><tr><td align=\"center\"><input type=\"submit\" value=\"BOOK LIST\"></td></tr>"; print "</table></form></td> </tr>"; ?> <tr> <td> <table width="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#7272B1" align="center"> <font style="font-size:12px;" face="Helvetica" color="#ffffff"><B>Your Cart</B></font></td> </tr> </table> </td> </tr> <?php $date = date('Y-m-d'); $result_cart = mysql_query("select book.title from tmp,book where tmp.user_id='$user' and tmp.sesid='$sesid' and tmp.date='$date' and book.item_no=tmp.item_no", $db); print "<tr><td align=\"center\"><table width=\"110\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">"; while ($row_cart=mysql_fetch_array($result_cart)) {    print "<tr><td align=\"center\"><font style=\"font-size:12px;\"    face=\"Helvetica\"    color=\"#000000\">$row_cart[0]</font></a></td></tr>"; } print "</table></td> </tr>"; ?> <tr> <td> <table width="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#7272B1" align="center"> <?php if (($login == "ok")  ($sesid)) print "<a href=\"checkout.php\"><font style=\"font-size:12px;\" face=\"Helvetica\" color=\"#ffffff\"><B>Check Out</B></font></a></td>"; ?> </tr> </table> </td> </tr> </table> </td> <td BGCOLOR="#1E237B" width="1" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height="1" width="1"> </td> 
end example
 

The above listing creates the middle section of the home page for the Client section.

Listing 9-24 shows the bottom section of the home page created using the bottomhtml.php file:

Listing 9-24: The bottomhtml.php File
start example
 <tr> <td HEIGHT="1" BGCOLOR="#1E237B" width="600" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1></td> </tr> </table> <!-- end table3--> </td> </tr> <tr><td> <table border=0 width=90%> <tr> <td align="center" colspan="3"><font face="arial" size="1" color="#1E237B">&nbsp;</font></td> </tr> </table> </td></tr> </table> </body> </html> 
end example
 

The above listing creates the bottom section of the home page for the online shopping cart application.

Figure 9-14 shows the home page of the online shopping cart application:

click to expand: this figure shows the interface created when the welcome.php file is processed.
Figure 9-14: Home Page of the Client Section

Registering a New User

To initiate the registration process, end user needs to click the New User link, as shown in Figure 9-2. The new.php file is executed by the Web server that represents a Web page to accept data for a new end user.

Listing 9-25 shows the content of the new.php file:

Listing 9-25: The new.php File
start example
 <?php include("tophtml.php"); ?> <tr> <td valign="top"> <table border="0" width="700" cellspacing="1" cellpadding="0" height="345"> <tr> <td BGCOLOR="#1E237B" width="1" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1> </td> <td width="540" valign="top"> <table border="0" cellpadding="2" cellspacing="2" width="100%"> <tr> <td> <table width=100%> <tr> <td align="center"> <form name="newusr" action="new1.php" method="GET"> <table border="0" cellpadding="2" cellspacing="2" width="100%"> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b>User Name *</b></font> </td> <td> <input type="text" name="user_id" maxlength="20" value="<?php if ($user_id) echo "$user_id"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b>Password *</b></font> </td> <td> <input type="password" name="passwd" maxlength="10" value="<?php echo "$passwd"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b>Confirm Password *</b></font> </td> <td> <input type="password" name="cpasswd" maxlength="10" value="<?php if ($cpasswd) echo "$cpasswd"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Name *</b></font> </td> <td> <input type="text" name="name" maxlength="40" value="< ?php if ($name) echo "$name"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Address1 *</b></font> </td> <td> <input type="text" name="address_line1"  value="<?php if ($address_line1) echo "$address_line1"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Address2</b></font> </td> <td> <input type="text" name="address_line2" value="< ?php if ($address_line2) echo "$address_line2";?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> City *</b></font> </td> <td> <input type="text" name="city" value="<?php if ($city) echo "$city"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Country *</b></font> </td> <td> <input type="text" name="country"  value="<?php if ($country) echo "$country"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Pin Code *</b></font> </td> <td> <input type="text" name="pin"  value="<?php if ($pin) echo "$pin"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Email ID *</b></font> </td> <td> <input type="text" name="email_id"  value="<?php if ($email_id) echo "$email_id"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Phone Number *</b></font> </td> <td> <input type="text" name="phone_number"  value="<?php if ($phone_number) echo "$phone_number";?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Fax Number </b></font> </td> <td> <input type="text" name="fax_number"  value="<?php if ($fax_number) echo "$fax_number"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Card Number *</b></font> </td> <td> <input type="text" name="card_no"  value="<?php if ($card_no) echo "$card_no"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Expiry Date</b></font> </td> <td> <input type="text" name="expiry_date"  value="<?php if ($expiry_date) echo "$expiry_date"; ?>"> </td> </tr> <tr> <td valign="top" align="right"> <font face="Verdana" size="1"><b> Card Type</b></font> </td> <td> <select name="card_type"> <option value="American Express">American Express</option> <option value="Master Card">Master Card</option> <option value="Visa">Visa</option> </select> <!-- <input type="text" name="card_type" value="<?php if ($card_type) echo "$card_type"; ?>"> --> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Submit"></td> </tr> </table> </form> </td> </tr> </table> </td> </tr> <tr> <td> </td> </tr> </table> </td> </tr> </table> </td> </tr> <?php include("bottumhtml.php"); ?> 
end example
 

The above listing creates a Web page to accept information to register a new end user, as shown in Figure 9-15:

click to expand: this figure shows the web page to register a new end user with the online shopping cart application. click the submit button to register the new end user with the online shopping cart application. all the fields marked with * are mandatory.
Figure 9-15: Web Page to Register New End User

When the end user clicks the Submit button, the new1.php file is processed to add the end user information to the database table.

Listing 9-26 show the content of the new1.php file:

Listing 9-26: The new1.php File
start example
 <?php settype($error,"string"); settype($order_list,"string"); settype($user,"string"); settype($sesid,"string"); $name=$_GET['user']; $user_id=$_GET['user_id']; $passwd=$_GET['passwd']; $address_line1=$_GET['address_line1']; $address_line2=$_GET['address_line2']; $city=$_GET['city']; $country=$_GET['country']; $pin=$_GET['pin']; $email_id=$_GET['email_id']; $phone_number=$_GET['phone_number']; $card_no=$_GET['card_no']; $expiry_date=$_GET['expiry_date']; $card_type=$_GET['card_type']; $fax_number=$_GET['fax_number']; /* echo "value ". $name; echo $user_id; echo $passwd; echo $address_line1; echo $address_line2; echo $city; echo $country; echo $pin; echo $email_id; echo $phone_number; echo $card_no; echo $expiry_date; echo $card_type; echo $fax_number; */ $db=mysql_connect('localhost','root',''); if (!$db) {    echo "Error When connecting to Database"; } mysql_select_db("shop", $db); $query = mysql_query("insert into user_profile(name,user_id,password,address_line1,address_line2,city,country,pin,email_id, phone_number,card_no,expiry_date,card_type,fax_number) values('$name','$user_id','$passwd','$address_line1','$address_line2','$city','$country', '$pin','$email_id','$phone_number','$card_no','$expiry_date','$card_type','$fax_number')",  $db) or ($error1=mysql_errno()); $num = mysql_affected_rows(); if ($num < 1)  $message="Agent ".$agentid." already exist."; else {    $user=$user_id;    session_save_path("/tmp");    session_start();    $sesid=session_id();    session_register("sesid");    session_register("order_list");    session_register("user");    $message = "You have registered successfully."; } include("tophtml.php"); ?> <tr> <td valign="top"> <table border="0" width="700" cellspacing="1" cellpadding="0" height="345"> <tr> <td BGCOLOR="#1E237B" width="1" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1> </td> <td width="540" valign="top"> <table border="0" cellpadding="2" cellspacing="2" width="100%"> <tr> <td> <table width=100%> <tr> <td align="center"> <table border="0" cellpadding="2" cellspacing="2" width="100%"> <tr> <td valign="top" align="center"> <font face="Verdana" size="2" color="#FF0000"><b><?php echo "$message" ?></b></font> </td> </tr> <?php if ($error1) {    print "    <tr>    <td valign=\"top\" align=\"center\">    <form name=\"form1\" action=\"new.php\" method=\"post\">    <input type=\"hidden\" name=\"name\" value=\"$name\">    <input type=\"hidden\" name=\"user_id\" value=\"$user_id\">    <input type=\"hidden\" name=\"address_line1\" value=\"$address_line1\">    <input type=\"hidden\" name=\"address_line2\" value=\"$address_line2\">    <input type=\"hidden\" name=\"city\" value=\"$city\">    <input type=\"hidden\" name=\"country\" value=\"$country\">    <input type=\"hidden\" name=\"pin\" value=\"$pin\"><input type=\"hidden\"    name=\"email_id\" value=\"$email_id\">    <input type=\"hidden\" name=\"phone_number\" value=\"$phone_number\">    <input type=\"hidden\" name=\"fax_number\" value=\"$fax_number\">    <input type=\"hidden\" name=\"card_no\" value=\"$card_no\">    <input type=\"hidden\" name=\"expiry_date\" value=\"$expiry_date\">    <input type=\"hidden\" name=\"card_type\" value=\"$card_type\">    <input type=\"submit\" value=\"Try Again\">    </td>    </tr>"; } ?> </table> </td> </tr> </table> </td> </tr> <tr> <td> </td> </tr> </table> </td> <?php include("right.php"); ?> </tr> </table> </td> </tr> <?php include("bottomhtml.php"); ?> 
end example
 

The above listing inserts a new record in the user_profile database table. If the new end user is registered successfully, the message, You have registered successfully, is displayed to the end user; else, an error message is displayed.

Viewing Books Stored in a Category

When the end user clicks the BOOK LIST button after selecting a book category from the Select Book Category combo box, as shown in Figure 9-14, the book_desc.php file is processed. This file displays all the books present in the specified category.

Listing 9-27 shows the content of the book_desc.php file:

Listing 9-27: Displaying Books in a Specified Category
start example
 <?php session_save_path("/tmp"); session_start(); include("tophtml.php"); $db=mysql_connect('localhost','root',''); if (!$db) {    echo "Error When connecting to Database"; } mysql_select_db("shop", $db); ?> <tr> <td valign="top"> <table border="0" width="700" cellspacing="1" cellpadding="0" height="345"> <tr> <td BGCOLOR="#1E237B" width="1" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1> </td> <td width="540" valign="top"> <table border="0" cellpadding="2" cellspacing="1" width="100%"> <tr> <td> <table width="100%" cellspacing="1" bgcolor="#FFFFFF"> <tr bgcolor="#0099CC"> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>BOOK NO.</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>CATEGORY</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>TITLE</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>AUTHOR</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>PRICE</b></font></td> <td align="center" width="20%">&nbsp;</td> </tr> <?php $req_from=$_GET['req_from']; $title=$_GET['title']; $search_by=$_GET['search_by']; $search_word=$_GET['search_word']; $item_type=$_GET['item_type']; if ($req_from == "search") {    if ($search_by == "author")    {       $result1=mysql_query("select * from book where author like '%$search_word%'", $db);    }    elseif ($search_by == "title")    {       $result1=mysql_query("select * from book where title like '%$search_word%'", $db);    } } else {    $result1=mysql_query("select * from book where item_type='$item_type'", $db); } $num = mysql_num_rows($result1); if ($num > 0) {    while($row1=mysql_fetch_array($result1))    {       print "<tr bgcolor=\"#F2F2F2\"><td align=\"center\" width=\"20%\"><font       face=\"verdana\" size=1       color=\"#000000\"><b>$row1[0]</b></font></td><td       align=\"center\"       width=\"20%\"><font face=\"verdana\" size=1       color=\"#000000\"><b>$row1[1]</b></font></td><td       align=\"center\" width=\"20%\"><font face=\"verdana\" size=1       color=\"#000000\"><b>$row1[2]</b></font></td><td       align=\"center\"       width=\"20%\"><font face=\"verdana\" size=1       color=\"#000000\"><b>$row1[3]</b></font></td><td       align=\"center\"       width=\"20%\"><font face=\"verdana\" size=1       color=\"#000000\"><b>$row1[4]</b></font></td><td       align=\"center\"       width=\"20%\"><form name=\"cart\" action=\"add_cart.php\"       action=\"get\"><input       type=\"hidden\" name=\"item_type\" value=\"$item_type\"><input type=\"hidden\"       name=\"item_no\" value=\"$row1[0]\"><input type=\"hidden\" name=\"req_from\"       value=\"$req_from\"><input type=\"hidden\" name=\"search_by\"       value=\"$search_by\"><input type=\"hidden\" name=\"search_word\"       value=\"$search_word\"><input type=\"submit\" value=\"ADD TO       CART\"></form></td></tr>";    } } else {    print "<tr bgcolor=\"#F2F2F2\"><td align=\"center\" width=\"20%\"    colspan=\"5\"><font face=\"verdana\" size=1 color=\"#FF0000\"><b>No match for    the $search_word found.</b></font></td></tr>"; } ?> </table> </td> </tr> <tr> <td> </td> </tr> </table> </td> <?php include("right.php"); ?> </tr> </table> 
end example
 

The above listing retrieves the information about books for a specific category.

Figure 9-16 shows the Web page that displays the books present in the Database category:

click to expand: this figure shows that two books are present in the database category.
Figure 9-16: Books in the Database Category

Placing a Book in the Shopping Cart

When the end user clicks the ADD TO CART button, shown in Figure 9-16, the add_cart.php file is processed to add the selected book to the cart.

Listing 9-28 shows the content of the add_cart.php file:

Listing 9-28: The add_cart.php File
start example
 <?php //include "sessioncheck.php"; session_start(); include("tophtml.php"); $date = date('Y-m-d'); $db=mysql_connect('localhost','root',''); if (!$db) {    echo "Error When connecting to Database"; } mysql_select_db("shop", $db); $user=$_SESSION["user"]; $sesid=$_SESSION["sesid"]; $search_by=$_GET['search_by']; $search_word=$_GET['search_word']; $item_type=$_GET['item_type']; $item_no=$_GET['item_no']; if ($user) {    if (!$resulr=mysql_query("insert into tmp    values('NULL','$user','$item_no','$sesid','$date')", $db))    {       $e=mysql_error();       echo "$e";    } } ?> <tr> <td valign="top"> <table border="0" width="700" cellspacing="1" cellpadding="0" height="345"> <tr> <td BGCOLOR="#1E237B" width="1" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1> </td> <td width="540" valign="top"> <table border="0" cellpadding="2" cellspacing="1" width="100%"> <tr> <td> <table width="100%" cellspacing="1" bgcolor="#FFFFFF"> <tr bgcolor="#0099CC"> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>BOOK NO.</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>CATEGORY</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>TITLE</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>AUTHOR</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>PRICE</b></font></td> <td align="center" width="20%">&nbsp;</td> </tr> <?php if ($req_from == "search") {    if ($search_by == "author")    {       $result1=mysql_query("select * from book where author like '%$search_word%'", $db);    }    elseif ($search_by == "title")    {       $result1=mysql_query("select * from book where title like '%$search_word%'", $db);    } } else {    $result1=mysql_query("select * from book where item_type='$item_type'", $db); } while($row1=mysql_fetch_array($result1)) {    print "<tr bgcolor=\"#F2F2F2\"><td align=\"center\" width=\"20%\"><font    face=\"verdana\" size=1    color=\"#000000\"><b>$row1[0]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row1[1]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row1[2]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row1[3]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row1[4]</b></font></td><td    align=\"center\" width=\"20%\"><form name=\"cart\" action=\"add_cart.php\"    action=\"post\"><input type=\"hidden\" name=\"item_type\"    value=\"$item_type\"><input type=\"hidden\" name=\"item_no\"    value=\"$row1[0]\"><input type=\"hidden\" name=\"req_from\"    value=\"$req_from\"><input type=\"hidden\" name=\"search_by\"    value=\"$search_by\"><input type=\"hidden\" name=\"search_word\"    value=\"$search_word\"><input type=\"submit\" value=\"ADD TO    CART\"></form></td></tr>"; } ?> </table> </td> </tr> <tr> <td> </td> </tr> </table> </td> <?php include("right.php"); ?> </tr> </table> </td> </tr> <?php include("bottomhtml.php"); ?> 
end example
 

The above listing adds a book to the shopping cart.

Figure 9-17 shows the output when the book, Relational Database Concepts, is added to the cart:

click to expand: this figure shows that the relational database concepts book has been added to the cart section.
Figure 9-17: Adding a Book to the Cart

Searching for a Book

The end user can search for a book either by its title or the author name. To search a book by title, enter the title in the Search By text field, as shown in Figure 9-17, and click the Search button. The book_desc.php file is executed to retrieve the books from the database that match the specified title.

Listing 9-29 shows the content of the book_desc.php file:

Listing 9-29: Retrieving Books matching a Specified Title
start example
 <?php session_save_path("/tmp"); session_start(); include("tophtml.php"); $db=mysql_connect('localhost','root',''); if (!$db) {    echo "Error When connecting to Database"; } mysql_select_db("shop", $db); ?> <tr> <td valign="top"> <table border="0" width="700" cellspacing="1" cellpadding="0" height="345"> <tr> <td BGCOLOR="#1E237B" width="1" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1> </td> <td width="540" valign="top"> <table border="0" cellpadding="2" cellspacing="1" width="100%"> <tr> <td> <table width="100%" cellspacing="1" bgcolor="#FFFFFF"> <tr bgcolor="#0099CC"> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>BOOK NO.</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>CATEGORY</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>TITLE</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>AUTHOR</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>PRICE</b></font></td> <td align="center" width="20%">&nbsp;</td> </tr> <?php $req_from=$_GET['req_from']; $title=$_GET['title']; $search_by=$_GET['search_by']; $search_word=$_GET['search_word']; $item_type=$_GET['item_type']; if ($req_from == "search") {    if ($search_by == "author")    {       $result1=mysql_query("select * from book where author like '%$search_word%'", $db);    }    elseif ($search_by == "title")    {       $result1=mysql_query("select * from book where title like '%$search_word%'", $db);    } } else {    $result1=mysql_query("select * from book where item_type='$item_type'", $db); } $num = mysql_num_rows($result1); if ($num > 0) {    while($row1=mysql_fetch_array($result1))    {       print "<tr bgcolor=\"#F2F2F2\"><td align=\"center\" width=\"20%\"><font       face=\"verdana\" size=1       color=\"#000000\"><b>$row1[0]</b></font></td><td       align=\"center\" width=\"20%\"><font face=\"verdana\" size=1       color=\"#000000\"><b>$row1[1]</b></font></td><td       align=\"center\" width=\"20%\"><font face=\"verdana\" size=1       color=\"#000000\"><b>$row1[2]</b></font></td><td       align=\"center\" width=\"20%\"><font face=\"verdana\" size=1       color=\"#000000\"><b>$row1[3]</b></font></td><td       align=\"center\" width=\"20%\"><font face=\"verdana\" size=1       color=\"#000000\"><b>$row1[4]</b></font></td><td       align=\"center\" width=\"20%\"><form name=\"cart\" action=\"add_cart.php\"       action=\"get\"><input type=\"hidden\" name=\"item_type\"       value=\"$item_type\"><input type=\"hidden\" name=\"item_no\"       value=\"$row1[0]\"><input type=\"hidden\" name=\"req_from\"       value=\"$req_from\"><input type=\"hidden\" name=\"search_by\"       value=\"$search_by\"><input type=\"hidden\" name=\"search_word\"       value=\"$search_word\"><input type=\"submit\" value=\"ADD TO       CART\"></form></td></tr>";    } } else {    print "<tr bgcolor=\"#F2F2F2\"><td align=\"center\" width=\"20%\"    colspan=\"5\"><font face=\"verdana\" size=1 color=\"#FF0000\"><b>No match for    the $search_word found.</b></font></td></tr>"; } ?> </table> </td> </tr> <tr> <td> </td> </tr> </table> </td> <?php include("right.php"); ?> </tr> </table>         </td> </tr> <?php include("bottomhtml.php"); ?> 
end example
 

Figure 9-18 shows the output when search is performed on the basis of the author name, Galvin:

click to expand: this figure shows the search result based on a specified criterion.
Figure 9-18: Searching Books Based on Author Name

Viewing Items Placed in a Shopping Cart

When the end user clicks the Check Out hyperlink present in the right section of the welcome.php Web page, the checkout.php file is executed. The checkout.php Web page creates a Web page that shows the books added to the shopping cart, as shown in Listing 9-30:

Listing 9-30: The checkout.php File
start example
 <?php session_start(); include("tophtml.php"); $date = date('Y-m-d'); ?> <tr> <td valign="top"> <table border="0" width="700" cellspacing="1" cellpadding="0" height="345"> <tr> <td BGCOLOR="#1E237B" width="1" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1> </td> <td width="540" valign="top"> <table border="0" cellpadding="2" cellspacing="1" width="100%"> <tr> <td> <table width="100%" cellspacing="1" bgcolor="#FFFFFF"> <tr bgcolor="#0099CC"> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>SNO.</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>BOOK NO.</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>CATEGORY</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>TITLE</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>AUTHOR</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>PRICE</b></font></td> <td align="center" width="20%">&nbsp;</td> </tr> <?php $sn=0; $total=0; $user=$_SESSION["user"]; $sesid=$_SESSION["sesid"]; $db=mysql_connect('localhost','root',''); if (!$db) {    echo "Error When connecting to Database"; } mysql_select_db("shop", $db); $result = mysql_query("select book.*,tmp.order_no from tmp,book where tmp.user_id='$user' and tmp.sesid='$sesid' and tmp.date='$date' and book.item_no=tmp.item_no", $db); while($row_chack=mysql_fetch_array($result)) {    $total=$total+$row_chack[4];    $sn++;    print "<tr bgcolor=\"#F2F2F2\"><td align=\"center\" width=\"20%\"><font    face=\"verdana\" size=1    color=\"#000000\"><b>$sn</b></font></td><td align=\"center\"    width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row_chack[0]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row_chack[1]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row_chack[2]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row_chack[3]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row_chack[4]</b></font></td><td    align=\"center\" width=\"20%\"><form name=\"cart\" action=\"del_cart.php\"    action=\"post\"><input type=\"submit\" value=\"DELETE\"><input type=\"hidden\"    name=\"order_no\" value='$row_chack[5]'></form></td></tr>"; } print "<tr bgcolor=\"#F2F2F2\"><td align=\"right\" width=\"100%\" colspan=\"2\"><form name=\"order\" action=\"final_order.php\" method=\"post\"><input type=\"submit\" value=\"POST YOUR ORDER\"></td><td align=\"right\" width=\"100%\" colspan=\"2\"><font face=\"verdana\" size=1 color=\"#000000\"><b>TOTAL BOOKS: &nbsp; $sn</b></font></td><td align=\"right\" width=\"100%\" colspan=\"2\"><font face=\"verdana\" size=1 color=\"#000000\"><b>TOTAL : &nbsp; </b></font></td><td align=\"left\" width=\"100%\"><font face=\"verdana\" size=1 color=\"#000000\"><b>$total</b></font></td></td></tr>"; ?> </table> </td> </tr> <tr> <td> </td> </tr> </table> </td> <?php include("right.php"); ?> </tr> </table> </td> </tr> <?php include("bottomhtml.php"); ?> 
end example
 

The above listing displays the titles of the books that are added to the shopping cart in a Web page, as shown in Figure 9-19 :

click to expand: this figure shows the books added to the shopping cart. the end user can click the delete button, adjacent to the added books, to remove the selected books from the shopping cart.
Figure 9-19: Displaying Books Added to the Shopping Cart

Removing a Book From the Shopping Cart

When the end user clicks the Delete button corresponding to a book name, as shown in Figure 9-19, the del_cart.php file is executed to remove the selected book from the shopping cart.

Listing 9-31 shows the content of the del_cart.php file:

Listing 9-31: The del_cart.php File
start example
 <?php session_start(); include("tophtml.php"); $date = date('Y-m-d'); $user=$_SESSION["user"]; $sesid=$_SESSION["sesid"]; $order_no=$_GET["order_no"]; $db=mysql_connect('localhost', 'root', ''); if (!$db) {    echo "Error When connecting to Database"; } mysql_select_db("shop", $db); $resul_del=mysql_query("delete from tmp where order_no=$order_no", $db); ?> <tr> <td valign="top"> <table border="0" width="700" cellspacing="1" cellpadding="0" height="345"> <tr> <td BGCOLOR="#1E237B" width="1" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1> </td> <td width="540" valign="top"> <table border="0" cellpadding="2" cellspacing="1" width="100%"> <tr> <td> <table width="100%" cellspacing="1" bgcolor="#FFFFFF"> <tr bgcolor="#0099CC"> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>SNO.</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>BOOK NO.</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>CATEGORY</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>TITLE</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>AUTHOR</b></font></td> <td align="center" width="20%"><font face="Arial" size=2 color="#ffffff"><b>PRICE</b></font></td> <td align="center" width="20%">&nbsp;</td> </tr> <?php $sn=0; $total=0; $result_chack = mysql_query("select book.*,tmp.order_no from tmp,book where tmp.user_id='$user' and tmp.sesid='$sesid' and tmp.date='$date' and book.item_no=tmp.item_no", $db); while($row_chack=mysql_fetch_array($result_chack)) {    $sn++;    $total=$total+$row_chack[4];    print "<tr bgcolor=\"#F2F2F2\"><td align=\"center\" width=\"20%\"><font    face=\"verdana\" size=1    color=\"#000000\"><b>$sn</b></font></td><td align=\"center\"    width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row_chack[0]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row_chack[1]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row_chack[2]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row_chack[3]</b></font></td><td    align=\"center\" width=\"20%\"><font face=\"verdana\" size=1    color=\"#000000\"><b>$row_chack[4]</b></font></td><td    align=\"center\" width=\"20%\"><form name=\"cart\" action=\"del_cart.php\"    action=\"post\"><input type=\"submit\" value=\"DELETE\"><input type=\"hidden\"    name=\"order_no\" value='$row_chack[5]'></form></td></tr>"; } print "<tr bgcolor=\"#F2F2F2\"><td align=\"right\" width=\"100%\" colspan=\"2\"><form name=\"order\" action=\"final_order.php\" method=\"post\"><input type=\"submit\" value=\"POST YOUR ORDER\"></td><td align=\"right\" width=\"100%\" colspan=\"2\"><font face=\"verdana\" size=1 color=\"#000000\"><b>TOTAL BOOKS: &nbsp; $j</b></font></td><td align=\"right\" width=\"100%\" colspan=\"2\"><font face=\"verdana\" size=1 color=\"#000000\"><b>TOTAL : &nbsp; </b></font></td><td align=\"left\" width=\"100%\"><font face=\"verdana\" size=1 color=\"#000000\"><b>$total</b></font></td></td></tr>"; ?> </table> </td> </tr> <tr> <td> </td> </tr> </table> </td> <?php include("right.php"); ?> </tr> </table> </td> </tr> <?php include("bottomhtml.php"); ?> 
end example
 

The above listing removes the selected book from the shopping cart.

Confirming an Order

When the end user clicks the POST YOUR ORDER button, as shown in Figure 9-19, the final_order.php file is executed to confirm the order.

Listing 9-32 shows the content of the final_order.php file:

Listing 9-32: The final_order.php File
start example
 <?php session_start(); include("tophtml.php"); $date = date('Y-m-d'); $user=$_SESSION["user"]; $sesid=$_SESSION["sesid"]; $order_no=$_GET["order_no"]; $db=mysql_connect('localhost','root',''); if (!$db) {    echo "Error When connecting to Database"; } mysql_select_db("shop", $db); $result_con=mysql_query("select user_id,item_no,date from tmp where sesid='$sesid' and user_id='$user' and date='$date'", $db); $tt=mysql_num_rows($result_con); if ($tt > 0) {    $result_ins=mysql_query("insert into transaction(order_no,user_id,date,status)    values('NULL','$user','$date','Pending')", $db);    $result_order=mysql_query("select max(order_no) from transaction where user_id='$user' and    date='$date'", $db);    $row_no = mysql_fetch_array($result_order);    $order_no = $row_no[0];    while($row_con=mysql_fetch_array($result_con))    {       $result11 = mysql_query("insert into order1       values('NULL','$order_no','$row_con[1]','$row_con[0]')", $db);    }    $result_del=mysql_query("delete from tmp where sesid='$sesid' and user_id='$user' and    date='$date'", $db); } ?> <tr> <td valign="top"> <table border="0" width="700" cellspacing="1" cellpadding="0" height="345"> <tr> <td BGCOLOR="#1E237B" width="1" background="/images/spacer.gif"> <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1> </td> <td width="540" valign="top"> <table border="0" cellpadding="2" cellspacing="1" width="100%"> <tr> <td> <table width="100%" cellspacing="1" bgcolor="#FFFFFF"> <tr bgcolor="#0099CC"> <td align="center" width="100%"><font face="Arial" size=2 color="#000000"><b> <?php if ($order_no) echo "Your order has been posted successfully. Your Order No. is $order_no"; else echo "Please select books for order."; ?> </b></font></td> </tr> </table> </td> </tr> <tr> <td> </td> </tr> </table> </td> <?php include("right.php"); ?> </tr> </table> </td> </tr> <?php include("bottomhtml.php"); ?> 
end example
 

The above listing confirms the order for the books added to the shopping cart.

Figure 9-20 shows the Web page that displays the order confirmation message to the end user:

click to expand: this figure shows the order confirmation web page. it also displays the order number for the placed order.
Figure 9-20: Web Page to Confirm the Order

Logging Off from the Shopping Cart Application

When the end user clicks the LOGOUT hyperlink present in the right section of welcome.php Web page, the logout.php file is executed, which logs the end user out of the application.

Listing 9-33 shows the content of the logout.php file:

Listing 9-33: The logout.php File for the End User
start example
 <?php session_save_path("/tmp"); session_start(); $sesid=session_id(); session_unregister("sesid"); session_unregister("order_list"); session_unregister("user"); session_destroy(); $sesid=""; include("tophtml.php"); ?> <tr> <td valign="top"> <table border="0" width="600" cellspacing="1" cellpadding="0" height="345"> <tr> <td BGCOLOR="#1E237B" width="1" background="/images/spacer.gif" <img SRC="/images/spacer.gif" ALT="-" BORDER=0 height=1 width=1> </td> <td width="540" valign="top"> <table border="0" cellpadding="2" cellspacing="2" width="100%"> <tr> <td> <table width=100%> <tr> <td align="center"><font face="arial" size=3 color="#FF0000"><b> Logout successfully.  </b></font></td> </tr> <?php include "bottumhtml.php"; ?> 
end example
 

The above listing logs out the end user from the online shopping cart application.




Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net