繁体   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