Jump to content
xisto Community

THC

Members
  • Content Count

    1
  • Joined

  • Last visited

  1. hello everybody, to those who are interested in creating interactive web page using PHP, this tutorial will describe how to build a simple shopping cart from A to Z you can either use a PHP editor or simply notepad to edit your pages, enjoy. first we'll design the database for the cart: make a new file and name it cart.sql and type this code: create table customers( id INT NOT NULL, first VARCHAR(32), mi CHAR(2), last VARCHAR(32), address1 VARCHAR(64), address2 VARCHAR(64), city VARCHAR(32), state VARCHAR(32), zip VARCHAR(10), country VARCHAR(32), shiptobilling VARCHAR(5), ship_address1 VARCHAR(64), ship_address2 VARCHAR(64), ship_city VARCHAR(32), ship_state VARCHAR(32), ship_zip VARCHAR(10), ship_country VARCHAR(32), ship_phone VARCHAR(32), email VARCHAR(128), PRIMARY KEY(id));create table order_details ( id INT NOT NULL, orderid INT, code VARCHAR(32), qty INT, PRIMARY KEY(id));create table orders( id INT NOT NULL, customer INT, status VARCHAR(16), tracking_number VARCHAR(128), PRIMARY KEY(id));create table inventory( id INT NOT NULL, name VARCHAR(32), category INT, code VARCHAR(32), description TEXT, price VARCHAR(8), picture VARCHAR(128), qty INT, PRIMARY KEY(id));create table category( id INT NOT NULL, name VARCHAR(128), description TEXT, PRIMARY KEY(id)); this file will be used to store the database and tables information and define any relationship configurations. now this is the PHP configuration file which will be used to define all the functions, global variables and webhosting settings, note that you will have to change some information to match your settings such as the paths of your files if you are using your own webserver or your webhosting info, usernames and passwords etc.. Put this file in /include dir under your wwwroot directory: <?define(SECURE_URL, "your webhost URL");define(IMAGE_URL, "your webhost URL/images/");define(SHIPPING_COST, "10.00"); // specify shipping cost heredefine(COMPANY_NAME, "your company name");define(CREDIT_AUTH_URL, "finishorder.php");define(COMPANY_EMAIL, "your email here");function connect() { ini_set("include_path", "any other path you want to include"); require_once("DB.php"); $type ="mysql"; $username = "your user name"; $password = "your password"; $host = "localhost"; $database = "cart"; $dsn = $type . "://" . $username . ":" . $password . "@" . $host . "/" . $database; $dbconn = DB::connect($dsn); $errortrap($dbconn); $dbconn->setFetchMode(DB_FETCHMODE_ASSOC); $return $dbconn;} // end function connectfunction errortrap($result) { if(DB::isError($result)) { ?><h3> There was an error!</h3><? die($result->getMessage()); }} // end function errortrapfunction alter_cart($cat, $items, $item, $action) { global $dbconn; $sql = "select * from inventory where id = '$item' AND category = '$cat'"; $result = $dbconn->query($sql); errortrap($result); if($result->numRows() > 0) { switch($action) { case("add"): if(!isset($items[$cat][$item])) { $items[$cat][$item] = 0; } $items[$cat][$item]++; break; case("remove"): if(isset($items[$cat][$item])) { $items[$cat][$item]--; } if($items[$cat][$item] < 1) { unset($items[$cat][$item]); } break; default: break; } } return $items; } // end function alter_cartfunction full_item($item, $items) { global $dbconn; $sql ="select * from inventory where id =$item"; $result = $dbconn->query($sql); errortrap($result); $result->fetchinto($r);?> <table border=1 cellpadding=5 cellspacing=0><tr><td class=tablehead><?=$r["name"]?></td></tr><tr><td><?=$r["name"]?><br><?=$r["description"]?><br><b>Price</b>: <?=$r["price"]?><p><?if($r["picture"] != "") { ?><div align="center"><img src="<? echo IMAGE_URL $r["picture"]?>"></div><?} // end ifif ($r["qty"] > 1) { ?><p><div align=center> <a href="<?=SECURE_URL?>cart.php?cat=<?=$r["category"]?>&item=<?=$item?>&itemview=<?=$item?>&action=add"><img src="<?=IMAGE_URL?>add.gif" border=0></a><? } else { ?> <p> Sorry, Out Of Stock </p> <? } ?><br><a href="<?=SECURE_URL?>cart.php">Return To List Of Items In This Category</a></div></td></tr></table> <?} // End function full_item function build_menu ($ref,$table) { global $dbconn; $sql = "select * from $table order by id"; $result = $dbconn->query($sql); errortrap($result); if($result->numRows() > 0) { $x=0; while($result->fetchInto($r)) { if($x==0) { echo '<option value="' . $r["id"] . '"selected>' . $r[$ref] . '</option>'; $x++; } else { echo '<option value="' . $r["id"] . '">' . $r[$ref] . '</option>'; } } } else { echo '<option value="">NO CATEGORIES DEFINED</option>';}} // end function build_menufunction head() { ?> <html> <head> <style type=text/css> h1, h2, h3, p, td {font-family: veranda, sans-serif;} .tablehead {font-size: 12pt; color: #FFFFFF; background-color: #000099; } .required {font-weight: bold; colr: red;} smalli {font-size: 8pt; font-style: italic;} </style> </head> <body bgcolor="#FFFFFF"> <div align=center> <table width="74%" border="0" cellspacing="0" cellpadding="0" height="128" bgcolor="#FFFFFF"> <tr> <td height ="134" align="center"><h1> Shopping Cart</h1></td> </tr> </table> <? }function calculate_total($items) { global $dbconn; $shipping = SHIPPING_COST; $total = 0; foreach($items as $key => $val) { foreach($items[$key] as $key2 => $val2) { $sql = "select * from inventory where id ='$key2'"; $result = $dbconn->query($sql); errortrap($result); $result->fetchinto($r); $total+= ($r["price"] * $val2); } } if($total != 0) { $total= $total + $shipping; } return($total);} // end function calculate_totalfunction display_cart($items) { global $dbconn; global $items, $status; $shipping = SHIPPING_COST; $count = 0; ?> <table border=1 cellpadding=5 cellspacing=0> <tr><td class=tablehead>Name</td><td clas=tablehead>Qty</td><td class=tablehead>Price Each</td><td class=tablehead> </td></tr> <? foreach($items[$cat] as $item => $qty) { $sql = "select * from inventory where id = '$item'"; $result = $dbconn->query($sql); errortrap($result); $result->fetchinto($r); ?> this the page where the database connection will be established then the customer can start shopping, search for products and edit his own cart. file name cart.php <?require_once("include/cart_inc.php");session_start();session_register("items");session_register("category_choice");session_register("total");if(!isset($items)) { $items= array();}if(!isset($category_choice)) { $category_choice=1;}/***************** MAIN *****************/head();$dbconn = connect();select_cat();$status = "shopping"; ?> <table width="58%" border="1" cellspacing="10" cellpadding="10" height="371" bordercolor="#0000FF" bgcolor="999999"><tr align ="left" valign="top"><td bgcolor="#CCCCCC" bordercolor="#0000FF"><table border="0" cellpadding="10"><tr><td valign=top><?if(isset($category_choice_in)) { $category_choice =$category_choice_in;}if(isset($update_cart)) { foreach($items_in as $cat => $val { foreach($items_in[$cat] as $id => $qty) { if ($qty < 1) { unset($items_in[$cat][$id]); } } } $items = $items_in}if(isset(itemsview)) { full_item($itemview, $items);} else { display_items($category_choice, $items);}if(isset($action)) { $items = alter_cart ($cat, $items, $item, $action);}?></td><td valign=top><h3> Your Cart: </h3><?if(isset($modify)) { edit_cart ($items);} else { display_cart ($items);}?><p><?if(sizeof($items) > 0 { $total = calculate_total ($items); print_r($items);?><p>Do you want to <a href="<?=SECURE_URL?>checkout.php"><b>checkout</b></a>?<?}?></td></tr></table></td></tr></table></div></body></html> This is the checkout page, file name checkout.php: <?require_once("include/cart_inc.php");session_start();head();$dbconn = connect();$response = "1"; // transaction okay//$response = "2"; // Declined Credit Card//$response = "3"; //General Errorif (sizeof($items)==0) {?><h3> There are no items in your cart! Click back to add some items to your cart.</h3><?} else {?><h2> Here are the items that you are ordering:</2><? $status = "checkout";display_cart ($items);?><p> Please fill in the following information to proceed.<p><FORM METHOD=POST ACTION = "<?=CREDIT_AUTH_URL?>"><INPUT TYPE=HIDDEN NAME="Amount" VALUE="<?$total?>"><INPUT TYPE=HIDDEN NAME="x_Description" VALUE="Order From <?=COMPANY_NAME?>"><INPUT TYPE=HIDDEN NAME="x_Invoice_Num" VALUE="<?=time()?>"><INPUT TYPE=HIDDEN NAME="x_response_code" VALUE="<?=$response?>"><?cart2form($items);?><table border="1" cellspacing="1" cellpadding="5"> <tr> <td colspan="2" class="tablehead"><b>BILLING ADDRESS</b>: </td> </tr> <tr> <td>Credit Card Number<span class="required">*</span></td> <td><input type="text" name="x_card_num"> </td></tr><tr> <td>Expiration Date<span class="required">*<br> (MMYY - for example 0402 for April 2002)</span></td><td><input type="text" name="x_exp_date" maxlength="4" size="4"></td></tr><tr> <td>First Name<span class="required">*</span></td> <td><input type="text" name="x_card_num"> </td></tr><tr> <td>Credit Card Number<span class="required">*</span></td> <td><input type="text" name="x_first_name"> </td></tr><tr> <td>Middle Initial</td> <td><input type="text" name="x_mi"> </td></tr><tr> <td>Last Name<span class="required">*</span></td> <td><input type="text" name="x_last_name"> </td></tr><tr> <td>Address Line 1<span class="required">*</span></td> <td><input type="text" name="x_address"> </td></tr><tr> <td>Address Line 2<span class="required">*</span></td> <td><input type="text" name="x_address2"> </td></tr><tr> <td>City<span class="required">*</span></td> <td><input type="text" name="x_city"> </td></tr><tr> <td>State or province<span class="required">*</span></td> <td><input type="text" name="x_state"> </td></tr><tr> <td>Zip/Postal Code<span class="required">*</span></td> <td><input type="text" name="x_zip" size="10" maxlength="10"> </td></tr><tr> <td>Country<span class="required">*</span></td> <td><select name="x_country"> <option>Saudi Arabia<option>Sudan<option>United Arab Emeritaes</select></td></tr><tr> <td>Daytime Phone Number<span class="required">*</span></td> <td><input type="text" name="x_phone"> </td></tr><tr> <td>Email<span class="required">*</span></td> <td><input type="text" name="x_email"> </td></tr><tr> <td>Shipping Address is as the same as Billing Address<span class="required">*</span></td> <td><input type="checkbox" name="shiptobilling" value="true"><br><font size="-2">(check to ship to your billing address)</font> </td></tr><tr> <td colspan="2" class="tablehead"> <p><b>SHIPPING ADDRESS</b><br>(Fill this out if your shipping address is different from your billing address):</p> </td></tr><tr> <td>Address Line 1<span class="required">*</span></td> <td><input type="text" name="x_address"> </td></tr><tr> <td>Address Line 2<span class="required">*</span></td> <td><input type="text" name="x_address2"> </td></tr><tr> <td>City<span class="required">*</span></td> <td><input type="text" name="x_city"> </td></tr><tr> <td>State or province<span class="required">*</span></td> <td><input type="text" name="x_state"> </td></tr><tr> <td>Zip/Postal Code<span class="required">*</span></td> <td><input type="text" name="x_zip" size="10" maxlength="10"> </td></tr><tr> <td>Country<span class="required">*</span></td> <td><select name="x_country"> <option>Saudi Arabia<option>Sudan<option>United Arab Emeritaes</select></td></tr></table><p><INPUT TYPE = "SUBMIT" value= "Submit Order"></p></form><?session_unset();session_destroy();} // end else statement?> I'll be coming back with rest of the code soon.
×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.