ความคิดเห็นที่ 3
ต่อค่ะ ========================================== ส่วนในไฟล์ _2Stemplate_cart_bottom
ไม่มีอะไรค่ะไว้แสดงส่วนท้ายของหน้าเวปเฉยๆๆ
และตัวปัญหาก็คือ ไฟล์นี้อ่ะค่ะ basket.php น่ะคะ
ตอนนี้ไม่มี error อ่ะไรค่ะ
แต่ปัญหาก็คือ จาทำยังไงให้เมื่อสั่งสินค้าเสร็จแล้ว
ให้มันส่งตัวแปรไปหน้า order.php
เพื่อจััดเก็บสินค้าที่สั่งและรายละเอียดทัั้งหมดลงฐานข้อมูล
ไฟล์ basket.php <?php //=================================================b1 error_reporting(E_ALL); session_start(); // สตาร์ทเซทซั่น include"ConnectionDb.php"; //เปิดการติดต่อฐานข้อมูล $expire = 3600; //กำหนดตัวแปร $expire $time = time(); //กำหนดตัวแปร $time $expire = $time + $expire; //สั่งคำนวณเวลาหมดอายุ $script_url = $_SERVER["PHP_SELF"]; //เก็บค่าชื่อสคริปต์
//===============================================b2 //ตรวจสอบ ถ้าไม่มีการกำหนดค่าตัวแปรเซสซั่น cart if(!isset($_SESSION['cart'])) { //เป็นจริง ตรวจสอบ ถ้าตัวแปรคุ๊กกี้ cart มีการกำหนดค่า if(isset($_COOKIE['cart'])) { //ให้ค่าตัวแปรเซสชั่น cart เก็บข้อมูลแบบอะเรย์ @$_SESSION['cart']=array(); //ทำการแปลงค่าคุ๊กกี้ cart ด้วยฟังก์ชั่น unserialize()เป็นค่าเดิม @$_SESSION['cart']=unserialize(stripslashes($_COOKIE['cart'])); //ให้ค่าตัวแปรเซสชั่น total_itemsเก็บค่าที่คำนวณได้จากฟังก์ชั่น Cart_calculate_items() @$_SESSION['total_items']=cart_calculate_items($_SESSION['cart']); //ให้ค่าตัวแปรเซสชั่น total_price เก็บค่าที่คำนวณได้จากฟังก์ชั่น cart_calculate_price() @$_SESSION['total_price']=cart_calculate_price($_SESSION['cart']); }else{ //ให้ค่าตัวแปรเซสชั่น cart เก็บข้อมูลแบบอาเรย์ $_SESSION['cart'] = array(); //ให้ค่าตัวแปรเซสชั่น total_item และ total_price เท่ากับ 0 $_SESSION['total_items']=0; $_SESSION['total_price']=0.00; //กำหนดให้ตัวแปร s_cart เท่ากับค่าที่แปลงไปเป็น bytestream ของตัวแปรเซสชั่น cart $s_cart=serialize($_SESSION['cart']); //กำหนดคุกกี้ชื่อว่า cart เก็บค่าจากตัวแปร $s_cart setcookie('cart',$s_cart,$expire); } } //=============================================b3 //============================================== //เพิ่มรายการสินค้า //============================================== //ถ้าตัวแปร action ที่ส่งผ่านมากับ URL เท่ากับ add if(@$_GET['action']=='add'){ //ถ่ายค่าตัวแปร pid ที่ส่งผ่าน url ไว้กับตัวแปร $item $item = $_GET['pid']; //ถ้าตัวแปรเซสชั่น cart กับรหัสสินค้าที่รับเข้ามามีการกำหนดค่าไว้ if(isset($_SESSION['cart'][$item])){ //เพิ่มทีละ 1 $_SESSION['cart'][$item]++; }else{ //ให้ค่าเท่ากับ 1 $_SESSION['cart'][$item]=1; } $_SESSION['total_items']=cart_calculate_items($_SESSION['cart']); $_SESSION['total_price']=cart_calculate_price($_SESSION['cart']); $s_cart = serialize($_SESSION['cart']); //สั่งให้ Redirect header("Location:$script_url?action=view"); exit; }
//================================================b4 //========================================== //ลบรายการสินค้า //========================================== //ถ้าตัวแปร action ที่ส่งผ่านมากับ URL เท่ากับ del if( @$_GET['action']=="del"){ //ถ่ายค่าตัวแปร pid ที่ส่งผ่าน URL ไว้กับตัวแปร $productID $ProductID=$_GET['pid']; unset($_SESSION['cart'][$ProductID]); $_SESSION['total_items']=cart_calculate_items($_SESSION['cart']); $_SESSION['total_price']=cart_calculate_price($_SESSION['cart']); $s_cart=serialize($_SESSION['cart']); header("Location:$script_url?action=view"); exit; }
//==============================================b5 //================================ //ปรับปรุงจำนวนสั่งซื้อ //================================ //ถ้าตัวแปร action ที่ส่งผ่านมากับ URL เท่ากับ update if(@$_REQUEST['action']=='update'){ //ถ่ายค่าตัวแปร pid ที่ส่งผ่าน URL ไว้กับตัวแปร $item $item=$_REQUEST['pid']; //ถ่ายค่าตัวแปร quantity ที่ส่งผ่าน URL ไว้กับตัวแปร $quantity $quantity=$_REQUEST['quantity']; if(isset($_SESSION['cart'][$item])){ $_SESSION['cart'][$item]=$quantity; }
$_SESSION['total_items']=cart_calculate_items($_SESSION['cart']); $_SESSION['total_price']=cart_calculate_price($_SESSION['cart']); $s_cart=serialize($_SESSION['cart']); header("Location:$script_url?action=view"); exit; } //=============================================b6 //========================= //ดูตระกร้ารถเข็น //========================= //ถ้าตัวแปร action ที่ส่งผ่านมากับ URL เท่ากับ VIEW if(@$_GET['action']=="view"){ //เรียกใช้งานฟังก์ชั่น cart_show() cart_show($_SESSION['cart'],$pictures='false',$editable = 'true'); }
//=============================================b7 //============================= //คำนวณจำนวณสินค้าทั้งหมด //============================= function cart_calculate_items($cart){ $items = 0; if(is_array($cart)){ foreach($_SESSION['cart'] as $ProductID =>$qty){ $items+=$qty;
} } return $items; }
//=============================================b8 //=================================== //คำนวณยอดเงินรวมทั้งสิ้น ของสินค้าที่ลูกค้าสั่งซื้อทั้งหมด //=================================== function cart_calculate_price($cart){ $price = 0.00; if(is_array($cart)){ foreach($cart as $ProductID => $qty){ $query = "SELECT ProductCostPerUnit FROM product WHERE ProductID ='$ProductID'"; $result = mysql_query($query);
if($result){ $row_result = mysql_fetch_assoc($result); $item_price = $row_result['ProductCostPerUnit']; $price +=$item_price*$qty; } } } return $price; } //================================================b9 //======================================== // สร้างคอนโทรลรายการตัวเลข จำนวนสินค้า //======================================== function qty_combo($qty_n,$pname){ echo " <select onChange=\"Update_qty('parent',this,0)\">\n"; for($i=1;$i<=20;$i++){ if($i==$qty_n){ echo "\t\t<option value=\"?action=update&pid=".$pname. "&quantity=$i\"selected>$i</option>\n"; } else { echo "\t\t<option value=\"?action=update&pid=".$pname. "&quantity=$i\">$i</option>\n"; } } echo " </select>\n"; } //================================================b10 //============================== //นำข้อมูลจากตาราง tbl_product ตามรหัสสินค้าที่รับเข้ามา //=============================== function cart_get_item_details($ProductID){ $query = "SELECT * FROM product WHERE ProductID='$ProductID'"; $result = mysql_query($query); $row_result = mysql_fetch_assoc($result); return$row_result; } //=================================================b11 //=================================== //สำหรับแสดงรายการสินค้าต่างๆ ในตระกร้า และแสดงตัวเลขคำนวณต่างๆ //=================================== function cart_show($cart,$pictures='false',$editable='true'){ include"_2Stemplate_cart_top.php"; $i=1; foreach($cart as $ProductID => $qty){ $product = cart_get_item_details($ProductID); $bgcolor = ($i++%2)?'#F2FAEB' : '#E6F2DF'; echo"<tbody>"; echo"<tr>"; echo" <td width=\"781\"><table width=\"608\" border=\"1\">"; echo" <td width=\"65\"><img src=\"img/Untitled-1.gif\" width=\"65\" height=\"26\"></td>"; echo"<td width=\"41\">"; echo"<a href=\"basket.php?action=del&pid=".$product['ProductID']."\">"; echo"<img src=\"images/icon_remove.gif\"alt=\"ลบรายการ'". $product['ProductName']."'\"border=\"0\">"; echo"</td>"; echo"<td width=\"170\" align=\"left\">".$product['ProductName']."</td>\n"; echo" <td width=\"50\">"; echo qty_combo($qty,$product['ProductID']); echo"</td>"; echo"<td width=\"81\">".number_format($product['ProductCostPerUnit'], 2,'.',',')."</td>\n"; echo"<td width=\"51\"><from method=\"post\" action=\"basket.php\"> "; echo" <input type=\"text\" name =\"discount\" size=\"1\">"; echo" <input type=\"submit\" value=\"dc\" size=\"1\"></td>"; echo" <td width=\"104\">".number_format(($qty*$product['ProductCostPerUnit']), 2,'.',',')."</td>\n"; echo"</td>"; echo"</tr>"; echo"</table>"; } echo " <td><table width=\"611\" height=\"148\" border=\"1\">"; echo " <tr>"; echo "<td width=\"65\"><img src=\"img/Untitled-1.gif\" width=\"65\" height=\"26\"></td>"; echo "<td width=\"403\" align=\"right\"><b>ภาษี7%</b></td>"; echo" <td width=\"109\" align=\"right\">"; $tax = $_SESSION['total_price']*0.07; echo number_format($tax,2,'.',','); echo" </td>"; echo "</tr>"; echo" <tr>"; echo"<td><img src=\"img/Untitled-1.gif\" width=\"65\" height=\"26\"></td>"; echo"<td align = right><b>รวมทั้งสิ้น</b>"; echo"</td>"; $tax = $_SESSION['total_price']*0.07; echo "<td align=\"right\">"; number_format($tax,2,'.',','); $paymoney = ($_SESSION['total_price']+$tax); echo number_format($paymoney,2,'.',','); echo "</td>"; echo" </tr>"; echo"</table>"; echo" <div align=center>\n"; echo" <p align=center>\n"; echo" <a href=\"_4Sfindproduct.php\"><img src=\"images/icon_con_shopping.gif\"alt=\"ไปซื้อสินค้าต่อ\" border=\"0\"></a>\n"; echo" <a href=\"order.php\"><img src=\"images/icon_payment.gif\"alt=\"ไปชำระเงิน\"border=\"0\"></p>\n"; echo" </div>\n"; include "_2Stemplate_cart_bottom.php"; } include"CloseDb.php";
echo"<a href=\"order.php?Oproductid=".$product['ProductID']."\">"; //echo"<a href=\"order.php?_productid=".$product['ProductID']."\">"; //&"_productname"=".$product['ProductName']."&"_productqty"="$qty"&"_productcost"=".$product['ProductCostPerUnit']."&"_tax"="$tax"&"_totalprice"=".$_SESSION['total_price']." ?>
จากคุณ :
noobestzy
- [
26 ส.ค. 51 17:43:00
A:117.47.93.234 X:
]
|
|
|