How to prevent adding duplicate items to the shopping cart

How to prevent adding duplicate items to the shopping cart

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
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
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
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
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
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
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
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
What is shopping cart?
admin panel and much more. How a shopping cart works? A shopping cart software...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
jsp code for shopping cart
jsp code for shopping cart  please provide me the jsp code for online shopping cart
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
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 Index Page
Shopping Cart Application What is Shopping Cart ? A shopping cart... and the product is added to his shopping cart. Introduction to Application Shopping Cart Features Database Design Creating Data Access Object (DAO
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 Application
Shopping Cart Application     ... with Simple Cart    ii) How to generate Simple Cart documentation   iii...; Introduction a) About the project b) How
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
ModuleNotFoundError: No module named 'django-shopping-cart'
: No module named 'django-shopping-cart' How to remove the ModuleNotFoundError: No module named 'django-shopping-cart' error? Thanks   Hi...ModuleNotFoundError: No module named 'django-shopping-cart'  Hi
ModuleNotFoundError: No module named 'gym-shopping-cart'
named 'gym-shopping-cart' How to remove the ModuleNotFoundError: No module named 'gym-shopping-cart' error? Thanks   Hi, In your...ModuleNotFoundError: No module named 'gym-shopping-cart'  Hi, My
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 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
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
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
Online Shopping Cart Services
Knowing About Online Shopping Cart Services The whole world has become... reasons, now people depend on e-commerce shopping cart. One of them is the security these websites provide. These shopping cart solutions offer the users to do
Shopping Cart Products
Know To Sell The Right Products On E-Commerce Shopping Cart E-commerce... differently can you present them with your e-commerce shopping cart? What kind of offers... as this is the fastest way of shopping and shipping products to customers. Many online
please give me an example about shopping cart using spring and hibernate
please give me an example about shopping cart using spring and hibernate  who can give me an ex about shoppingcart using spring and hibernate intergration ? thanks alot
E-commerce: Shopping cart
E-commerce: Shopping cart        ...;ADS_TO_REPLACE_1 E-Commerce ? role of a Shopping Cart Life has...;  Features of a shopping cart Easy-to-use store management tools
where do I find the fix for tree view catalog not expanding and collapsing correctly for roseindia shopping cart 1.1
correctly for roseindia shopping cart 1.1  where do I find the fix for tree view catalog not expanding and collapsing correctly for roseindia shopping cart 1.1
What to Find in E-Commerce Shopping Cart Software ?
What to Find in E-Commerce Shopping Cart Software The e-commerce shopping...-friendly, you have to select the right e-commerce shopping cart software... the user and also smooth operation of your business. The shopping cart
How to prevent the page refresh while submitting the form??
How to prevent the page refresh while submitting the form??  Hi. Requirement: There are two drop down boxes.Dropdown2 should load corresponding... every time when changing Dropdown1. Is there any way in JAVA SCRIPT to prevent
Drop Down Reloads again in IE..How to prevent this?
Drop Down Reloads again in IE..How to prevent this?  Hi i was using two drop down box..One for Displaying date followed by another for Dispalying.....This is the problem im facing in IE but it works well in Firefox..How to overcome this IE issue
how can we prevent back option after log out.
how can we prevent back option after log out.  how can we prevent back option after log out
how to validate duplicate records in struts1
how to validate duplicate records in struts1  Hi, After submitting the form i have to validate the email id. If already exists in database i have to display an error message saying that email id already exist
how to validate duplicate records in struts1
how to validate duplicate records in struts1  Hi, After submitting the form i have to validate the email id. If already exists in database i have to display an error message saying that email id already exist
how to prevent user selecting past date from calender
how to prevent user selecting past date from calender  how to prevent user selecting past date from calender ex: today's date is 21st march user should not select 20th march or below
Open Source Shopping Cart
-of-stock items If the items are not in stock, e-commerce shopping cart...Open Source Shopping Cart Open Source Shopping carts software... portal then you can choose any of the good open source shopping cart software
add to cart
add to cart  sir, i want to do add to cart to my shopping application each user using sessions. Plz help thnaks in advance
online shopping - Java Beginners
online shopping  Respected Sir, Sir please help me how to handle online shooping and shopping cart by click on image by only using jsp
how to display duplicate elements with out using collection Frame work?
how to display duplicate elements with out using collection Frame work?  how to display duplicate elements with out using collection Frame work
how to prevent from navigating Back after logout or session expire in jsp
how to prevent from navigating Back after logout or session expire in jsp  Hii Sir, I am making a web app in which after logout one can go to back pages from the browsers back button which i dont want in my app. Plz

Ads