php cart. tables, folder and files 3 tables(orders, orders_detail and products) folder img for store...

40
PHP Cart

Upload: douglas-hoover

Post on 02-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

PHPCart

Tables, folder and files• 3 Tables(Orders, Orders_detail and Products)• Folder img for store all picture• Product.php for show all product• Order.php for receive order• Show.php for show data in cart• Delete.php for delete data from cart• Checkout.php for submit data• Save_checkout.php for save all data into table• Clear.php for clear all data from cart• Cal_p.php for increase order qty in cart• Cal_m.php for decrease order qty from cart• Finish_order.php for show message transaction finish• View_order for preview invoice• And include.php for connect database

CREATE TABLE Orders

CREATE TABLE `orders` (`OrderID` int(5) unsigned zerofill NOT NULL auto_increment,`OrderDate` datetime NOT NULL,`Name` varchar(100) NOT NULL,`Address` varchar(500) NOT NULL,`Tel` varchar(100) NOT NULL,`Email` varchar(100) NOT NULL,PRIMARY KEY (`OrderID`)) ENGINE=MyISAM DEFAULT CHARSET=utf8

AUTO_INCREMENT=3 ;

Insert Data Into Orders

INSERT INTO `orders` VALUES (00001, '2013-08-30 09:59:13', 'Rungsan Suwannahong', '39 RMUTT Thailand', '0211245647', '[email protected]');

INSERT INTO `orders` VALUES (00002, '2013-08-30 10:15:03', 'Rungsan Suwannahong', '39 RMUTT Thailand', '0211245647', '[email protected]');

CREATE TABLE ORDERS_DETAIL

CREATE TABLE `orders_detail` (`DetailID` int(5) NOT NULL auto_increment,`OrderID` int(5) unsigned zerofill NOT NULL,`ProductID` int(4) NOT NULL,`Qty` int(3) NOT NULL,PRIMARY KEY (`DetailID`)) ENGINE=MyISAM DEFAULT CHARSET=utf8

AUTO_INCREMENT=5 ;

Insert Data Into Orders_detail

INSERT INTO `orders_detail` VALUES (1, 00001, 4, 1);INSERT INTO `orders_detail` VALUES (2, 00002, 3, 3);INSERT INTO `orders_detail` VALUES (3, 00002, 1, 1);INSERT INTO `orders_detail` VALUES (4, 00002, 4, 1);

CREATE TABLE Product

CREATE TABLE `product` (`ProductID` int(4) NOT NULL auto_increment,`ProductName` varchar(100) NOT NULL,`Price` double NOT NULL,`Picture` varchar(100) NOT NULL,PRIMARY KEY (`ProductID`)) ENGINE=MyISAM DEFAULT CHARSET=utf8

AUTO_INCREMENT=5 ;

Insert Data Into Product

INSERT INTO `product` VALUES (1, 'Product 1', 100, '1.gif');INSERT INTO `product` VALUES (2, 'Product 2', 200, '2.gif');INSERT INTO `product` VALUES (3, 'Product 3', 300, '3.gif');INSERT INTO `product` VALUES (4, 'Product 4', 400, '4.gif');

Product.php

<?//session_start();//session_destroy();?><html><head><meta http-equiv="Content-Type" content="text/html;

charset=utf-8"></head>

<?include ("include.php");$strSQL = "SELECT * FROM product";$objQuery = mysql_query($strSQL) or die(mysql_error());?><table width="327" border="1"> <tr> <td width="101">Picture</td> <td width="101">ProductID</td> <td width="82">ProductName</td> <td width="79">Price</td> <td width="37">Cart</td> </tr>

<? while($objResult = mysql_fetch_array($objQuery)) { ?> <tr><td><img src="img/<?=$objResult["Picture"];?>" width=50 ></td> <td><?=$objResult["ProductID"];?></td> <td><?=$objResult["ProductName"];?></td> <td><?=$objResult["Price"];?></td> <td><a href="order.php?ProductID=<?=$objResult["ProductID"];?>">Order</a></td> </tr>

<? } ?></table><br><br><a href="show.php">View Cart</a> | <a

href="clear.php">Clear Cart</a><?mysql_close();?></body></html>

Order.php

<?ob_start();session_start();if(!isset($_SESSION["intLine"])){

$_SESSION["intLine"] = 0; $_SESSION["strProductID"][0] = $_GET["ProductID"]; $_SESSION["strQty"][0] = 1; header("location:show.php");

}

else{

$key = array_search($_GET["ProductID"],$_SESSION["strProductID"]);if((string)$key != ""){

$_SESSION["strQty"][$key] = $_SESSION["strQty"][$key]+ 1;}else{$_SESSION["intLine"] = $_SESSION["intLine"] + 1;$intNewLine = $_SESSION["intLine"];$_SESSION["strProductID"][$intNewLine]=$_GET["ProductID"];

$_SESSION["strQty"][$intNewLine] = 1;} header("location:show.php");

}?>

Show.php

<?session_start();?><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><?include ("include.php");?><table width="400" border="1">

<tr> <td width="101">ProductID</td> <td width="82">ProductName</td> <td width="82">Price</td> <td width="79">Qty</td> <td width="79">Total</td> <td width="10">Del</td> <td width="10">Add</td> <td width="10">minus</td> </tr> <? $Total = 0; $SumTotal = 0; for($i=0;$i<=(int)$_SESSION["intLine"];$i++) {

if($_SESSION["strProductID"][$i] != "") {$strSQL = "SELECT * FROM product WHERE ProductID = '".$_SESSION["strProductID"][$i]."' ";$objQuery = mysql_query($strSQL) or die(mysql_error());$objResult = mysql_fetch_array($objQuery);$Total = $_SESSION["strQty"][$i] * $objResult["Price"];$SumTotal = $SumTotal + $Total; ?> <tr><td><?=$_SESSION["strProductID"][$i];?></td><td><?=$objResult["ProductName"];?></td><td><?=$objResult["Price"];?></td><td><?=$_SESSION["strQty"][$i]; ?></td><td><?=number_format($Total,2);?></td>

<td><a href="delete.php?Line=<?=$i;?>">x</a></td> <td ><a href=

"cal_p.php?ProductID=<?=$objResult["ProductID"];?>">+</a></td> <td ><a href= "cal_m.php?ProductID=<?=$objResult["ProductID"];?>">-

</a></td> </tr> <? }

} ?></table>Sum Total <?=number_format($SumTotal,2);?><br><br><a href="product.php">Go to Product</a>

<?if($SumTotal > 0){

?>| <a href="checkout.php">CheckOut</a>

<?}

?><?mysql_close();?></body></html>

Delete.php

<?ob_start();session_start();

$Line = $_GET["Line"];$_SESSION["strProductID"][$Line] = "";$_SESSION["strQty"][$Line] = "";

header("location:show.php");?>

Checkout.php

<?session_start();?><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-

8"></head><?include ("include.php");?>

<table width="400" border="1"> <tr> <td width="101">ProductID</td> <td width="82">ProductName</td> <td width="82">Price</td> <td width="79">Qty</td> <td width="79">Total</td> </tr> <? $Total = 0; $SumTotal = 0; for($i=0;$i<=(int)$_SESSION["intLine"];$i++) {

if($_SESSION["strProductID"][$i] != "") {

$strSQL = "SELECT * FROM product WHERE ProductID = '".$_SESSION["strProductID"][$i]."' ";

$objQuery = mysql_query($strSQL) or die(mysql_error());$objResult = mysql_fetch_array($objQuery);$Total = $_SESSION["strQty"][$i] * $objResult["Price"];$SumTotal = $SumTotal + $Total;

?> <tr>

<td><?=$_SESSION["strProductID"][$i];?></td><td><?=$objResult["ProductName"];?></td><td><?=$objResult["Price"];?></td><td><?=$_SESSION["strQty"][$i];?></td><td><?=number_format($Total,2);?></td>

</tr>

<? }

} ?></table>Sum Total <?=number_format($SumTotal,2);?><br><br><form name="form1" method="post" action="save_checkout.php"> <table width="304" border="1"> <tr> <td width="71">Name</td> <td width="217"><input type="text" name="txtName"></td> </tr>

<tr> <td>Address</td> <td><textarea name="txtAddress"></textarea></td> </tr> <tr> <td>Tel</td> <td><input type="text" name="txtTel"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="txtEmail"></td> </tr> </table> <input type="submit" name="Submit" value="Submit"></form>

<?mysql_close();?></body></html>

Save_checkout.php<?session_start();include ("include.php"); $Total = 0; $SumTotal = 0; $strSQL = "

INSERT INTO orders (OrderDate,Name,Address,Tel,Email)VALUES ('".date("Y-m-d H:i:s")."','".$_POST["txtName"]."','".$_POST["txtAddress"]."' ,'".$_POST["txtTel"]."','".$_POST["txtEmail"]."') ";

mysql_query($strSQL) or die(mysql_error()); $strOrderID = mysql_insert_id();

for($i=0;$i<=(int)$_SESSION["intLine"];$i++) {

if($_SESSION["strProductID"][$i] != "") { $strSQL = "INSERT INTO orders_detail(OrderID,ProductID,Qty) VALUES ('".$strOrderID."','".$_SESSION["strProductID"][$i]."','".$_SESSION["strQty"][$i]."') "; mysql_query($strSQL) or die(mysql_error()); }

}mysql_close();session_destroy();header("location:finish_order.php?OrderID=".$strOrderID);?>

Clear.php

<?ob_start();session_start();session_destroy();

header("location:show.php");?>

Cal_p.php<?ob_start();session_start();

$key = array_search($_GET["ProductID"], $_SESSION["strProductID"]);if((string)$key != ""){ $_SESSION["strQty"][$key] = $_SESSION["strQty"][$key] + 1;} header("location:show.php");

?>

Cal_m.php<?ob_start();session_start();

$key = array_search($_GET["ProductID"], $_SESSION["strProductID"]);if((string)$key != ""){ $_SESSION["strQty"][$key] = $_SESSION["strQty"][$key]-1;} header("location:show.php");

?>

Finish_order.php

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-

8"></head><body>Finish Your Order. <br><br><a href="view_order.php?OrderID=<?=$_GET["OrderID"];?>">View

Order</a></body></html>

View_order.php

<html><head><meta http-equiv="Content-Type"content="text/html; charset=utf-8"></head><?include ("include.php");$strSQL = "SELECT * FROM orders WHERE OrderID = '".

$_GET["OrderID"]."' ";$objQuery = mysql_query($strSQL) or die(mysql_error());$objResult = mysql_fetch_array($objQuery);?>

<table width="304" border="1"> <tr> <td width="71">OrderID</td> <td width="217">

<?=$objResult["OrderID"];?></td> </tr> <tr> <td width="71">Name</td> <td width="217">

<?=$objResult["Name"];?></td> </tr> <tr> <td>Address</td> <td><?=$objResult["Address"];?></td> </tr>

<tr> <td>Tel</td> <td><?=$objResult["Tel"];?></td> </tr> <tr> <td>Email</td> <td><?=$objResult["Email"];?></td> </tr> </table> <br><table width="400" border="1"> <tr> <td width="101">ProductID</td> <td width="82">ProductName</td> <td width="82">Price</td>

<td width="79">Qty</td> <td width="79">Total</td> </tr><?$Total = 0;$SumTotal = 0;$strSQL2 = "SELECT * FROM orders_detail WHERE OrderID = '".$_GET["OrderID"]."' ";$objQuery2 = mysql_query($strSQL2) or die(mysql_error());while($objResult2 = mysql_fetch_array($objQuery2)){

$strSQL3 = "SELECT * FROM product WHERE ProductID = '".$objResult2["ProductID"]."' ";$objQuery3 = mysql_query($strSQL3) or die(mysql_error());$objResult3 = mysql_fetch_array($objQuery3);

$Total = $objResult2["Qty"] * $objResult3["Price"];$SumTotal = $SumTotal + $Total;

?> <tr>

<td><?=$objResult2["ProductID"];?></td><td><?=$objResult3["ProductName"];?></td><td><?=$objResult3["Price"];?></td><td><?=$objResult2["Qty"];?></td><td><?=number_format($Total,2);?></td>

</tr> <?

} ?></table>

Sum Total <?=number_format($SumTotal,2);?><?mysql_close();?><p><a href=product.php>back to main</a></body></html>

Include.php

<?mysql_connect("localhost","root","1234");mysql_select_db("test");?>