簡體   English   中英

購物車會議php

[英]shopping cart Session php

    <?php

session_start(); 
print '<h1>Your Shopping Cart:</h1>';
print '<h4>The Product ID: '.$_SESSION['id'].' </h4>';
print '<h4>The Quantity: '.$_SESSION['quantity'].' </h4>';

$dbc = mysql_connect('127.0.0.1', 'root', ''); 
mysql_select_db('product', $dbc);

$query = "SELECT * FROM prod WHERE id='".$_SESSION['id']."'";



if ($r = mysql_query($query, $dbc)) {

while ($row = mysql_fetch_array($r)) { 
print "<p><b>Product Name: {$row['name']}<br />
             The Price: {$row['price']}<br />
             Shipping Cost: {$row['shipping']}<br />



 </p><hr />\n";
 }}
?>

這個代碼用於購物車與Session,但問題是它只保留一個產品。

例如,如果您購買產品A並在購物車產品B中刪除B然后添加A產品請幫助我我想添加多個產品並在屏幕上打印##

在購物車中添加其他級別:

$_SESSION['cart'][$productID]['quantity'];
                 ^^^^^^^^^^^^^

所以你保留每個產品的數據。 現在你正在使用一個單獨的級別,當你添加新產品時,你會不斷覆蓋它。

跟php數組打個招呼: http ://www.php.net/manual/en/book.array.php :)
基本上不是:

$_SESSION['id'] = 1234;

你會想要:

$_SESSION['products'][] = array('id'=>1234, 'quantity'=>10);

然后你會迭代$ _SESSION ['products']

foreach($_SESSION['products'] AS $product){
   echo $product['id'];
}

您應該將購物車保存在單個會話變量$_SESSION['cart'] 添加新產品時,您可以使用此產品,

$_SESSION['cart'] = array();         //initialize once
array_push($_SESSION['cart'], array("id" => $id, "quantity" => $q));
print_r($_SESSION['cart']);

這樣,您就可以在購物車中擁有多種產品。

對於特定的框架CodeIgniter,有一個提供購物車功能的類。 看看這個

$_SESSION['cart'] = array();
$_SESSION['cart']['1'] = 2;//add product A,id=1,quality=2
$_SESSION['cart']['2'] = 3;//add product B,id=2,quality=3

//get all items
foreach($_SESSION['cart'] as $item)
{
    print_r($item);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM