|
 |
|
เกี่ยวกับตะกร้าสินค้าแบบ session ครับผม รบกวนให้คำปรึกษาหน่อยครับ |
|
 |
|
|
 |
 |
|
สคริปตัวข้างล่างนี้ได้ทำตามหนังสือครับ มันมีส่วนไม่เป็นไปตามความต้องการคือ
1.มันไม่ยอมบวกค่าจำนวนสินค้าเพิ่มขึ้น เมื่อมีการซื้อสินค้ารหัสเดิมซ้ำอีกครั้ง
ตัวอย่างซื้อสินค้านะครับ
รหัส 1 เป็นจำนวน 1 ชิ้น
รหัส 2 เป็นจำนวน 2 ชิ้น
ปัญหาที่พบก็คือ เมื่อกลับไปสู่หน้าหลักสินค้า โดยต้องการซื้อสินค้ารหัส 1 เพิ่มอีก ก็ทำการหยิบใส่ตะกร้า อีกครั้ง
ปรากฏว่าพอไปหน้าตะกร้าสินค้า มันไม่ยอมบวกจำนวนสินค้าขึ้น
จากเดิม
รหัส 1 เป็นจำนวน 1 ชิ้น
รหัส 2 เป็นจำนวน 2 ชิ้น
ผลลัพธ์ที่ถูกต้องคือ
ใหม่
รหัส 1 เป็นจำนวน 2 ชิ้น
รหัส 2 เป็นจำนวน 2 ชิ้น
Code (PHP)
session_start();
require_once("include/class.db.php");
require_once("include/initail.php");
$db = new DB("$hostname","$user","$pass","$dbase");
$pro_id = $_REQUEST['pro_id'];
if(!is_numeric($_POST['amount'])){exit("<script>alert('Please number input!!!');history.back();</script>");}
session_register("sess_id");
session_register("sess_name");
session_register("sess_price");
session_register("sess_num");
if(count($sess_id) == "0"){
$check = 1;
}else if(!in_array($pro_id,$sess_id)){
$check = 1;
}
if($check == 1){
$select = $db->select("*","tb_product","pro_id = '$pro_id'","","","");
$row = $db->fetch_arr($select);
$sess_id[] = $row['pro_id'];
$sess_name[] = $row['pro_name'];
$sess_price[] = $row['pro_price'];
$sess_num[] = $_POST['amount'];
}
exit("<script type=\"text/javascript\">window.location='basket.php';</script>");
// header("Location : basket.php");
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2010-11-08 15:06:29 |
By :
ช่วยด้วย |
View :
1011 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือบอกหลักการนะครับ ก่อนที่จะทำการเพิ่ม session ใหม่ก็จะต้องทำการตรวจสอบว่า product_id ตัวนั้น ๆ มีอยู่ใน session หรือยัง โดยใช้การวนลูปใน array ของ session ครับ ถ้ามีอยู่แล้วก็ใช้การบวกจำนวนที่สั่งน่ะครับ 
|
 |
 |
 |
 |
Date :
2010-11-08 17:01:55 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|