|
 |
|
รบกวนเรื่อง session[cart] หน่อยครับ..หน้าแสดงสินค้า (show_product) จะเป็น การเรียกข้อมูลในฐานข้อมูลมาตารางเดียว โดยไม่มีการกดเลือกใดๆทั้งสิ้น เป็นแค่ detial |
|
 |
|
|
 |
 |
|
หน้าแสดงสินค้า (show_product) จะเป็น การเรียกข้อมูลในฐานข้อมูลมาตารางเดียว โดยไม่มีการกดเลือกใดๆทั้งสิ้น เป็นแค่ detial แสดงข้อมูลสินค้า ซึ่งมี ชื่อสินค้า ราคา อยู่ในนั้นแล้ว
เสร็จแล้วก็ทำลิงค์พร้อมกับส่งค่าไปหน้า cart (?id=<?=$array['id']?>) พอเสร็จแล้วผมได้เขียนคำสั่งในการเพิ่มสินค้าโดยอ้างถึง ID ในฐานข้อมูล ที่รับค่ามาจากหน้าที่แล้ว (show_product) โดยเขียนฟังค์ชั่นการทำงานไว้ดังนี้
<?php
session_start();
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
if ($cart) {
$cart .= ','.$_GET['id'];
} else {
$cart = $_GET['id'];
}
break;
case 'delete':
if ($cart) {
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($_GET['id'] != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
case 'update':
if ($cart) {
$newcart = '';
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$id = str_replace('qty','',$key);
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($id != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
for ($i=1;$i<=$value;$i++) {
if ($newcart != '') {
$newcart .= ','.$id;
} else {
$newcart = $id;
}
}
}
}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
?>
แล้วทำการแสดงค่าปรกติโดยใช้ foreach
ปัญหาครับ ถ้าผมจะสร้าง selectbox สำหรับเลือกค่าที่ไม่ได้อยู่ในฐานข้อมูลเลย (เป็นค่าตายตัวที่อยู่ในโค๊ด) จะเอา Data ดังกล่าวไปรวมไว้ใน session ในโค๊ดส่วนบนที่ผมได้ทำการเขียนไว้แล้วได้อย่างไรครับ
ซึ่งผมได้ลองทำการเพิ่มค่าตัวแปร session เข้าไปในโค๊ดส่วนบน มันทำให้ โปรแกรมมีปัญหาผิดเพี้ยนไปจากเดิมครับ
ซึ่งปัจจุบัน ตอนนี้ผมได้ทำการสร้างฐานข้อมูลไว้สำหรับเก็บค่า ที่เลือกใน show_product ไว้ในฐานข้อมูล (selectbox) แต่กรณีที่ลูกค้าใช้กันหลายๆคน ข้อมูลที่เลือกนั้นมันจะเปลี่ยนตามค่าที่ได้ ครั้งสุดท้าย เช่น
ลูกค้าคนที่ 1 เลือกสินค้า ที่มี id=20 หนึ่งรายการ โดยเปลี่ยนแปลงข้อมูลใน selectbox option ที่ 1 ก่อนหยิบใส่ตระกร้า (cart)
ต่อมา
ลูกค้าคนที่ 2 เข้าใช้ในเวลาเดียวกัน เลือกสินค้ารายการเดียวกับลูกค้าคนที่ 1 (แบบไม่รู้ตัว) คือ สินค้าที่มี id=20 และเปลี่ยนแปลงค่าใน selectbox option เป็น 3
ในตะกร้าของ ลูกค้าคนที่ 1 ที่เลือก สินค้าที่มี id=20 รายการใน select box จะเปลี่ยนเป็น option ที่ 3 โดยทันที ครับ ไม่ทราบว่า มีใครพอจะให้คำแนะนำผมได้บ้าง
ผมต้องการให้ ข้อมูลที่ลอยๆไปเก็บไว้ใน session พร้อมกับข้อมูลที่ถูกเลือกไว้ในฐานข้อมูล เพื่อที่จะ foreach ได้ข้อมูลออกมาได้พร้อมๆกันได้อย่างไรครับ
ลองเข้าไปทดสอบโปรแกรมได้ที่
http://www.oneairtools.com/Hachi/page/product.php?id=1
Tag : PHP, CakePHP

|
|
 |
 |
 |
 |
Date :
2011-04-26 13:06:56 |
By :
Dragons_first |
View :
1506 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สงสัยจะอธิบายไม่เข้าใจยากเกิ๊นไป๋
|
 |
 |
 |
 |
Date :
2011-04-27 08:49:35 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|