javascript 如何使用 PHP 会话创建一个简单的购物车?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23073185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a simple shopping cart using PHP sessions?
提问by Jerielle
I am creating a cart for my page and this is my first time that I am creating a shopping cart. I have an understanding in PHP sessions and how to use them but I am having a difficulty in creating my cart.
我正在为我的页面创建一个购物车,这是我第一次创建购物车。我对 PHP 会话以及如何使用它们有所了解,但我在创建购物车时遇到了困难。
So far here's what I have.
到目前为止,这就是我所拥有的。
$category = $_GET['cat'];
$product_id = $_GET['product'];
$product_detail = get_allproduct_detail($product_id); //query to get product info
$prod_price = $product_detail['prod_price'];
$sale_price = $product_detail['sale_price'];
$prod_type = $product_detail['prod_type'];
Here's the product part
这是产品部分
<div id="main_item_img">
<img src="/ahm/images/aroma/large/<?php echo $category; ?>/<?php echo get_product_img($_GET['product']); ?>.jpg" />
<div id="magnify">
<div class="zoom fitInBox">Fit in the Box</div>
<div class="zoom originalSize">Original Size</div>
<div class="zoom zoomin">zoom +</div>
<div class="zoom zoomout">zoom -</div>
</div>
</div>
<form method="POST" action="">
<div id="item_detail_right">
<label>Qty:<input type="text" name="qty" value="1" size="5" style="text-align: center" />
<input type="button" value="+Cart" />
<input type="button" value="+Wishlist" id="mywishlist" data-wishlist-id="<?php echo $_GET['product']; ?>" />
<input type="hidden" name="product_price" value="<?php echo $prod_price; ?>" />
<input type="hidden" name="sale_price" value="<?php echo $sale_price; ?>" />
<input type="hidden" name="product_id" value="<?php echo $_GET['product']; ?>" />
<input type="hidden" name="product_name" value="<?php echo strtoupper(get_product_name($_GET['product'])); ?>" />
</div>
</form>
<div id="cart_list">
<!-- This should be display the cart list -->
</div>
How can I create a function for adding/updating prices and removing cart item? What I want is after the user click the add to cart. It will simple display a section or div in the same page displaying the product cart and the total. Do i need to setup an AJAX for this or simple pure PHP code? I am also a beginner in AJAX process.
如何创建用于添加/更新价格和删除购物车项目的功能?我想要的是在用户单击添加到购物车之后。它将简单地在显示产品购物车和总数的同一页面中显示一个部分或 div。我是否需要为此或简单的纯 PHP 代码设置 AJAX?我也是 AJAX 流程的初学者。
How can I do this? Can you give me an example of this?
我怎样才能做到这一点?你能给我举个例子吗?
回答by madi
This is what you need to do:
这是你需要做的:
Learn how to use session: http://www.w3schools.com/php/php_sessions.asp
Store the selected line items (object/array) in the session. For example: array(array('pAAA','2',550,"img/pAAA.jpg"),array('pAA1','2',550,"img/pAA1.jpg")); is stored in the named session called shopping_card.
At the product part page where you display the card, do the following:
了解如何使用 session:http: //www.w3schools.com/php/php_sessions.asp
在会话中存储选定的行项目(对象/数组)。例如: array(array('pAAA','2',550,"img/pAAA.jpg"),array('pAA1','2',550,"img/pAA1.jpg")); 存储在名为shopping_card 的命名会话中。
在显示卡片的产品部件页面上,执行以下操作:
<?php if(isset($_SESSION['shopping_card'])): ?> //check if session is set ?> <div id="cart_list"> <?php foreach($_SESSION['shopping_card'] as $product): ?> <!-- This should be display the cart list --> <?php end foreach;?> </div> <?php endif; ?>
<?php if(isset($_SESSION['shopping_card'])): ?> //check if session is set ?> <div id="cart_list"> <?php foreach($_SESSION['shopping_card'] as $product): ?> <!-- This should be display the cart list --> <?php end foreach;?> </div> <?php endif; ?>
Make sure that you have started the session. So everytime the user post something to the shopping card, the page refreshed.Hope it helps. I did not really check the syntax. My main objective is to give you the flow on how you should get started.
确保您已开始会话。所以每次用户在购物卡上发布东西时,页面都会刷新。希望它有所帮助。我没有真正检查语法。我的主要目标是让您了解应该如何开始。