Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa PHP How to prevent adding duplicate items to the shopping cart
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Lakshesh Verma
How to prevent adding duplicate items to the shopping cart
2 Answer(s)      10 months ago
Posted in : PHP


this is saved as index.php

  <html>
  <head>
  <title>Catalog</title>
  <link rel="stylesheet" href="mm_travel2.css" type="text/css" />
   </head>
   <body bgcolor="#C0DFFD">
  <tr>
<td width="40">&nbsp;</td>
<td colspan="2" valign="top">&nbsp;<br />
&nbsp;<br />
<table border="0" cellspacing="0" cellpadding="2" width="610">

     <h1 align="left">Super Simple Shopping Cart<h1>
    <tr>
      <td><img src="sherlock-holmes-set-of-5-books.jpeg" alt="Sherlock Holmes" width="200" height="150" border="3" /></td>
      <td>&nbsp;</td>
      <td><img src="transcend-jm1066ksn-2g-400x400-imad57wzgufgxuhy.jpeg" alt="Jetram RAM" width="200" height="150" border="3" /></td>
      <td>&nbsp;</td>
      <td><img src="hp.jpeg" alt="HP Pen Drive" width="200" height="150" border="3" /></td>
      </tr>
    <tr>
      <td><a href="order.php?add=Sherlock Holmes(Set of 5 books)&price=336.00&qty=1">Sherlock Holmes(Set of 5 books)</a><br />
      Price: $336.00</td>
      <td>&nbsp;</td>
       <td><a href="order.php?add=JetRam Laptop 2GB RAM&price=839.00&qty=1">JetRam Laptop 2GB RAM</a><br />
      Price: $839.00</td>
     <td>&nbsp;</td>
       <td><a href="order.php?add=HP 8GB Pen Drive&price=363.00&qty=1">HP 8GB Pen Drive</a><br />
      Price: $363.00</td>
     </tr>
    <tr>
        <td colspan="7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
    </tr>

    <tr>
      <td><img src="c-the-com.jpeg" alt="C++ Book" width="200" height="150" border="3" /></td>
      <td>&nbsp;</td>
      <td><img src="samsung.jpeg" alt="Memory Card" width="200" height="150" border="3" /></td>
      <td>&nbsp;</td>
      <td><img src="panasonic-sr-wa-10-sr-wa-10-400x400-imadbgg7ebyz6wyf.jpeg" alt="Electric Cooker" width="200" height="150" border="3" /></td>
      </tr>
    <tr>
      <td><a href="order.php?add=C++ Complete Reference Book&price=474.00&qty=1">C++ Complete Reference Book</a><br />
      Price: $474.00</td>
      <td>&nbsp;</td>
       <td><a href="order.php?add=Samsung 4GB Memory Card&price=299.00&qty=1">Samsung 4GB Memory Card</a><br />
      Price: $299.00</td>
     <td>&nbsp;</td>
       <td><a href="order.php?add=Panasonic Electric Cooker&price=1000.00&qty=1">Panasonic Electric Cooker</a><br />
      Price: $1,000.00</td>
    </tr>

    <tr>
        <td colspan="7">&nbsp;</td>
    </tr>
  </table>    </td>
<td width="100%">&nbsp;</td>
   </tr>

   <tr>
 <td>&nbsp;</td>
   <td><a href="order.php">View Cart</a></td>
   <td>&nbsp;</td>
<td>&nbsp;</td>
 </tr>
</table>
 </body>
  </html>

this is saved as order.php

     <?php 
   session_start();
   if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }
   if (isset($_GET['itemID']) && isset($_GET['price']) && isset($_GET['qty'])){
$ITEM = array(
'name' => $_GET['itemID'], 
'price' => $_GET['price'], 
'qty' => $_GET['qty']       
    );
      $_SESSION['SHOPPING_CART'][] =  $ITEM;
      header('Location: ' . $_SERVER['PHP_SELF']);
      }
   else if (isset($_GET['remove'])){
   unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
    header('Location: ' . $_SERVER['PHP_SELF']);
   }
  else if (isset($_GET['empty'])){
 session_destroy();
 header('Location: ' . $_SERVER['PHP_SELF']);
}
 else if (isset($_POST['update'])) {
 foreach ($_POST['items_qty'] as $itemID => $qty) {
 if ($qty == 0) {
  unset($_SESSION['SHOPPING_CART'][$itemID]); 
    }
  else if($qty >= 1) {
$_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty; 
}
else if($qty < 0)
{
  echo "*....Quantity must be 1 or more";
 }
 }
  //header('Location: ' . $_SERVER['PHP_SELF']);
 } 


  ?>
  <html>
   <head>
   <title>Online Order Form</title>
   </head>
    <body>
  <h1>Shopping Cart</h1>
 <form action="" method="post" name="shoppingcart">
 <table width="500" border="1">
  <tr>
    <th>&nbsp;</th>
    <th>Item Name</th>
    <th>Unit Price</th>
    <th>Qty</th>
    <th>Cost</th>
  </tr>
  <?php 
    $GrandTotal=0;
    foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
    $GrandTotal = $GrandTotal + ($item['qty']*$item['price']);

     ?>

    <tr id="item<?php echo $itemNumber; ?>">    
        <td><a href="?remove=<?php echo $itemNumber; ?>">Remove</a></td>
        <td><?php echo $item['name']; ?></td>
        <td><?php echo $item['price']; ?></td>
        <td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="2" maxlength="3" /></td>
        <td><?php echo $item['qty'] * $item['price']; ?></td>       
    </tr>
    <?php
    }
    ?>

</table>
<p>
<table>
<tr id="item<?php echo $itemNumber; ?>">
<td><p><strong>Grand Total: $<?php echo $GrandTotal;?></strong></p><td>
    </tr>
   </table>

    <label>
    <input type="submit" name="update" id="update" value="Update Cart" />
     </label>
     </p>

    </form>
     <p><a href="index.php">Continue Shopping</a> - <a href="?empty">Empty Cart</a>      </p>
     </div>
    </body>
     </html>
   help me as soon as possible
Thanks in advance for the help

Advertisement
View Answers

February 22, 2014 at 5:36 PM


Hi,

Before adding into the array you should check if the item is already present?

You can iterate the items through array and then add the items to the array if it is not present.

Thanks



February 24, 2014 at 11:48 AM


thanks for providing me the solution... But will you please explain it with the help of the code because I am Beginner to php and this is my first try to any php assignment...

Thanks in advance



Related Tutorials/Questions & Answers:
How to prevent adding duplicate items to the shopping cart
How to prevent adding duplicate items to the shopping cart  ...;?php session_start(); if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); } if (isset($_GET['itemID']) && isset($_GET
Shopping Cart
Shopping Cart  Give some same code for shopping cart.. once click "Add to cart button" add item into cart and also moved into next step
Advertisements
shopping cart
shopping cart  hi i want the sample project for shopping cart in struts with mysql ,if any one knows means please help me
Shopping Cart
it to the shopping cart. Shopping cart software allows you to view the items in your cart...Shopping Cart Shopping cart is also know as trolley, carriage, buggy or wagon is a cart mostly used while shopping in shopping mall. These cart is provided
how can i prevent duplicate records using servlets and MVC model?
how can i prevent duplicate records using servlets and MVC model?  hai, I need a program to insert values into database at the same time it shows error msg on response page while we are giving duplicate entry of a primary key.i
Shopping Cart,Shopping Cart software
Shopping Cart       Overview A web based shopping cart is something like the original grocery shop shopping cart that is used by the customer in selecting certain
How to create dynamic buttons for adding products on a cart.
How to create dynamic buttons for adding products on a cart.  Hi. I have some problems creating a page to add items into a cart. The page loads dynamic products from a database, and i would like to know how to group this products
Shopping Cart
Shopping Cart In this section we will discuss about shopping cart and how you can use different open source shopping cart for hosting your shopping... the shopping cart, we will also see how to setup a shopping cart website for selling
What is shopping cart?
and much more. How a shopping cart works? A shopping cart software is dynamic web...What is shopping cart? In this tutorial we will understand the shopping cart. We will explain you what is a shopping cart? Why it is so popular these days
Free Java Shopping Cart,Shopping cart Application
Free Java Shopping Cart       Shopping Cart allows the web site owner to setup online store so that visitors can... basic information about shopping cart and the software that help online
Choosing the Best Ecommerce Shopping Cart
Choosing the Best Ecommerce Shopping Cart In this articles explains you how to choose the best ecommerce shopping cart for your next online shopping portal... Ecommerce Shopping Cart for running your ecommerce portal. You should select e
jsp code for shopping cart
jsp code for shopping cart  please provide me the jsp code for online shopping cart
web hosting shopping cart
web hosting shopping cart  What is web hosting shopping cart? Please... for hosting the shopping cart application. Since shopping cart application... hosting companies specialized in shopping cart hosting will help you configuring
Web hosting with shopping cart
hosting with shopping cart? Can I use any cheap hosting provider to host my shopping cart? Why we should choose hosting company specialized in shopping cart hosting? Thanks   Hi, What is the meaning of Web hosting with shopping cart
Shopping cart
Shopping cart Overview A web based shopping cart is something like the original grocery shop shopping cart that is used by the customer... information at the checkout counter. Software Shopping cart is used around the world
To create a shopping cart
To create a shopping cart  I want to create a online shopping cart using struts and hibernate. I created my first page. Now i want to provide..., how to retrieve the data from the database by creating a single table
Features of shopping cart
Features of shopping cart In this section we will learn the features of good shopping cart applications. There are shopping cart application on the Internet... on requirement and you can select best shopping cart application for your shopping
Shopping cart Application
Shopping Cart Application     ... with Simple Cart    ii) How to generate Simple Cart documentation   iii...; Introduction a) About the project b) How
Web hosting with shopping cart
Web hosting with shopping cart If you are looking for hosting your shopping... web hosting company you should use for hosting your shopping cart applications. Which hosting with shopping cart support should be used and what
Shopping Cart Index Page
Shopping Cart Application What is Shopping Cart ? A shopping cart is an application which runs on the server and allows users to do online shopping... is added to his shopping cart. Introduction to Application Shopping Cart
jsp shopping cart - JSP-Servlet
jsp shopping cart  i develope the online shopping project. but in that i require the code for how to insert a item into cart and also delete from it and how could I add more than one item into the cart. please give me an idea
i need project for shopping cart in struts 1 with the oracle database and give clear explanation for how to execute it in my eclipse
i need project for shopping cart in struts 1 with the oracle database and give clear explanation for how to execute it in my eclipse  i need a project for shopping cart in struts1 with the oracle database and give clear
Shopping Cart Features
Ideal Requirements Of Shopping Cart Most of the people think to use a shopping.... Using an shopping cart for your e-commerce site (Online Store) is just... store and make sure of that. Focus on the basic feature of the shopping cart
Shopping Cart design
Shopping Cart design We provide Shopping Cart designing, development and maintenance services. We design develops world class shopping cart system that can... source shopping cart for our client. We customize the shopping cart for meet
shopping cart using sstruts - Struts
shopping cart using sstruts  Hi, This is question i asked ,u send one link.but i cant able to ddownload the file .please send the code file. -------------- I need the example programs for shopping cart using struts
Shopping Cart Application installation manual missing
Shopping Cart Application installation manual missing  Hi, I downloaded this Shopping cart zip folder from http://www.roseindia.net/struts... of how to get the app up and running. Urgent help please and thanks a lot
online shopping cart complete coding in pure jsp
online shopping cart complete coding in pure jsp  online shopping cart complete coding in pure jsp   Please visit the following link: JSP Online shopping cart
E-commerce shopping cart hosting
E-commerce shopping cart web hosting In this article we will explain you how to find best hosting company for hosting E-Commerce shopping cart. The good ecommerce shopping cart web hosting company is very import in success of online
Error in Shopping Cart project - Development process
Error in Shopping Cart project  I tried running the shopping cart project given in the below link: http://www.roseindia.net/shoppingcart... when I submit "http://localhost:8080/cart" I received the following error
Online Shopping Cart Solutions
Professional Shopping Cart Developers Can Bring Yield Great Profits There are different shopping cart solutions available to suite different online stores. What is important is that one must look for custom shopping cart solutions
Maximize Sales By Setting up Your Shopping Cart
Maximize Sales By Setting up Your Shopping Cart Setting up a shopping cart... of the tools available in a wise manner. The shopping cart solution comes in varied... to and even works along with other features of the shopping cart solution
 

 

 

DMCA.com