最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create a simple shopping cart using PHP sessions? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

How can I do this? Can you give me an example of this?

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.

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.

How can I do this? Can you give me an example of this?

Share Improve this question asked Apr 15, 2014 at 1:33 JerielleJerielle 7,55029 gold badges103 silver badges168 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

This is what you need to do:

  1. Learn how to use session: http://www.w3schools./php/php_sessions.asp

  2. 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.

  3. At the product part page where you display the card, do the following:

 <?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.

发布评论

评论列表(0)

  1. 暂无评论