购物车会话php [英] shopping cart Session php

查看:100
本文介绍了购物车会话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的购物车,但问题在于它仅保留一种产品.

This code for shopping cart with Session but the problem is that it keeps only one product.

例如,如果您购买产品A,而在购物车产品B中删除B,则添加产品A 请帮我 我想添加一种以上的产品并打印在屏幕上##

For example, if you purchase a product A and in Cart product B delete B then add A product Please help me I want to add more than one product and printed on the screen ##

推荐答案

向php数组问好: http://www.php.net/manual/en/book.array.php :)
基本上代替:

Say hello to php arrays: http://www.php.net/manual/en/book.array.php :)
Basically instead of :

$_SESSION['id'] = 1234;

您将要:

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

然后,您将像

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

这篇关于购物车会话php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆