shopping cart code:
Expand|Select|Wrap|Line Numbers
- <?
- include("db.php");
- include("functions.php");
- if($_REQUEST['command']=='delete' && $_REQUEST['id']>0){
- remove_product($_REQUEST['id']);
- }
- else if($_REQUEST['command']=='clear'){
- unset($_SESSION['cart']);
- }
- else if($_REQUEST['command']=='update'){
- $max=count($_SESSION['cart']);
- for($i=0;$i<$max;$i++){
- $id=$_SESSION['cart'][$i]['id'];
- $q=intval($_REQUEST['Bags'.$id]);
- if($q>0 && $q<=999){
- $_SESSION['cart'][$i]['qty']=$q;
- }
- else{
- $msg='Some products not updated!, quantity must be a number between 1 and 999';
- }
- }
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Shopping Cart</title>
- <script language="javascript">
- function del(pid){
- if(confirm('Do you really mean to delete this item')){
- document.form1.id.value=id;
- document.form1.command.value='delete';
- document.form1.submit();
- }
- }
- function clear_cart(){
- if(confirm('This will empty your shopping cart, continue?')){
- document.form1.command.value='clear';
- document.form1.submit();
- }
- }
- function update_cart(){
- document.form1.command.value='update';
- document.form1.submit();
- }
- </script>
- </head>
- <body>
- <form name="form1" method="post">
- <input type="hidden" name="id" />
- <input type="hidden" name="command" />
- <div style="margin:0px auto; width:600px;" >
- <div style="padding-bottom:10px">
- <h1 align="center">Your Shopping Cart</h1>
- <input type="button" value="Continue Shopping" onclick="window.location='products.php'" />
- </div>
- <div style="color:#F00"><?=$msg?></div>
- <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
- <?
- if(is_array($_SESSION['cart'])){
- echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Id</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
- $max=count($_SESSION['cart']);
- for($i=0;$i<$max;$i++){
- $id=$_SESSION['cart'][$i]['id'];
- $q=$_SESSION['cart'][$i]['qty'];
- $bag_name=get_bag_name($id);
- if($q==0) continue;
- ?>
- <tr bgcolor="#FFFFFF"><td><?=$i+1?></td><td><?=$bag_name?></td>
- <td>$ <?=get_price($id)?></td>
- <td><input type="text" name="Bags<?=$id?>" value="<?=$q?>" maxlength="3" size="2" /></td>
- <td>$ <?=get_price($id)*$q?></td>
- <td><a href="javascript:del(<?=$id?>)">Remove</a></td></tr>
- <?
- }
- ?>
- <tr><td><b>Order Total: $<?=get_order_total()?></b></td><td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()"><input type="button" value="Update Cart" onclick="update_cart()"><input type="button" value="Place Order" onclick="window.location='billing.php'"></td></tr>
- <?
- }
- else{
- echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
- }
- ?>
- </table>
- </div>
- </form>
- </body>
- </html>
Products code:
Expand|Select|Wrap|Line Numbers
- <?
- include("db.php");
- include("functions.php");
- if($_REQUEST['command']=='add' && $_REQUEST['id']>0){
- $id=$_REQUEST['id'];
- addtocart($pid,1);
- header("location:shoppingcart.php");
- exit();
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Products</title>
- <script language="javascript">
- function addtocart(id){
- document.form1.productid.value=id;
- document.form1.command.value='add';
- document.form1.submit();
- }
- </script>
- </head>
- <body>
- <form name="form1">
- <input type="hidden" name="id" />
- <input type="hidden" name="command" />
- </form>
- <div align="center">
- <h1 align="center">Bags</h1>
- <table border="0" cellpadding="2px" width="600px">
- <?
- $result=mysql_query("select * from Bags");
- while($row=mysql_fetch_array($result)){
- ?>
- <tr>
- <td><img src="<?=$row['picture']?>" /></td>
- <td> <b><?=$row['bag_name']?></b><br />
- <?=$row['designer']?><br />
- Price:<big style="color:green">
- $<?=$row['price']?></big><br /><br />
- <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['id']?>)" /> </td>
- </tr>
- <tr><td colspan="2"><hr size="1" /></td>
- <? } ?>
- </table>
- </div>
- </body>
- </html>
function.php
Expand|Select|Wrap|Line Numbers
- <?
- function get_product_name($id){
- $result=mysql_query("select bag_name from Bags where id=$id");
- $row=mysql_fetch_array($result);
- return $row['name'];
- }
- function get_price($id){
- $result=mysql_query("select price from Bags where id=$id");
- $row=mysql_fetch_array($result);
- return $row['price'];
- }
- function remove_product($id){
- $id=intval($id);
- $max=count($_SESSION['cart']);
- for($i=0;$i<$max;$i++){
- if($id==$_SESSION['cart'][$i]['id']){
- unset($_SESSION['cart'][$i]);
- break;
- }
- }
- $_SESSION['cart']=array_values($_SESSION['cart']);
- }
- function get_order_total(){
- $max=count($_SESSION['cart']);
- $sum=0;
- for($i=0;$i<$max;$i++){
- $id=$_SESSION['cart'][$i]['id'];
- $q=$_SESSION['cart'][$i]['qty'];
- $price=get_price($id);
- $sum+=$price*$q;
- }
- return $sum;
- }
- function addtocart($id,$q){
- if($id<1 or $q<1) return;
- if(is_array($_SESSION['cart'])){
- if(product_exists($id)) return;
- $max=count($_SESSION['cart']);
- $_SESSION['cart'][$max]['id']=$id;
- $_SESSION['cart'][$max]['qty']=$q;
- }
- else{
- $_SESSION['cart']=array();
- $_SESSION['cart'][0]['id']=$id;
- $_SESSION['cart'][0]['qty']=$q;
- }
- }
- function product_exists($id){
- $id=intval($id);
- $max=count($_SESSION['cart']);
- $flag=0;
- for($i=0;$i<$max;$i++){
- if($id==$_SESSION['cart'][$i]['id']){
- $flag=1;
- break;
- }
- }
- return $flag;
- }
- ?>