<?
# smallcart.php
# ปรับปรุง : 10 กันยายน 2547 โดย บุรินทร์ รุจจนพันธุ์ 0-1992-7223
# โปรแกรมนี้ไม่สมบูรณ์ เพราะต้องการให้นักศึกษานำไปศึกษา และปรับปรุงให้ใช้กับ mysql
# เดิมโปรแกรมนี้ใช้ cookie อยู่ในหนังสือ perlphpasp.com บทที่ 18
# =======================
session_start();
if (!isset($_SESSION['allproduct'])) $_SESSION['allproduct'] = "";
if (isset($_GET['action'])) {
switch ($_GET['action']) {
case "removeall":
$_SESSION['allproduct'] = "";
break;
case "add":
$_SESSION['allproduct'] = $_SESSION['allproduct'].":".$_GET['item'].",".$_GET['quantity'].",".$_GET['price'];
$aproduct = $_SESSION['allproduct'];
break;
case "view":
$aproduct = $_SESSION['allproduct'];
break;
case "remove":
$aproduct="";
$getline = preg_split("/[:]/",$_SESSION['allproduct']);
for ($j=1;$j<count($getline);$j++) {
if ($_GET['removenumber'] <> $j) $aproduct = "$aproduct:$getline[$j]";
}
$_SESSION['allproduct'] = $aproduct;
break;
case "sendorderbymail":
$aproduct = $_SESSION['allproduct'];
mail($_GET['tomail'],"Order from smallcart","$aproduct","From: ".$_GET['frommail']);
break;
}
# =======================
echo "<hr>Action : ".$_GET['action'];
if ($_GET['action'] == "add" || $_GET['action'] == "view" || $_GET['action'] == "remove") {
$tpay = 0;
echo "<ol>";
$getline = preg_split("/[:]/",$aproduct);
for ($j=1;$j<count($getline);$j++) {
$rec = preg_split("/[,]/",$getline[$j]);
echo "<li>Product: $rec[0] Quantity: $rec[1] Price: $rec[2]";
$tpay = $tpay + ($rec[1] * $rec[2]);
}
echo "</ol>";
echo "Total pay :". $tpay;
}
}
# =======================
# Data structure : item,price
$pro = "book,30:pen,2:car,100000";
$getline = preg_split("/[:]/",$pro);
for ($j=0;$j<count($getline);$j++) {
$rec = preg_split("/[,]/",$getline[$j]);
echo "<form action=".$_SERVER{'SCRIPT_NAME'}." method=get>";
echo "<input type=text name=quantity value=1>";
echo "<b>$rec[0]</b> price: $rec[1]";
echo "<input type=hidden name=item value=".$rec[0].">";
echo "<input type=hidden name=price value=".$rec[1].">";
echo "<input type=submit value=add name=action></form>";
}
?>
<a href=?action=removeall>Remove All</a> | <a href=?action=view>View</a><br>
<form action="<?=$_SERVER{'SCRIPT_NAME'};?>" method=get>
Number: <input type=text name=removenumber value=1>
<input type=submit value=remove name=action>
</form>
<form action="<?=$_SERVER{'SCRIPT_NAME'};?>" method=get>
To seller: <input type=text name=tomail value=burin@yonok.ac.th size=40><br>
From customer: <input type=text name=frommail value=webmaster@thaiall.com size=40>
<br><input type=submit value=sendorderbymail name=action>
</form>
จำนวน : 72 บรรทัด