473,323 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Can you help with my shopping cart?

I can only get one item in the cart. When I attempt to insert a new item in the cart the old item is overwritten. I've placed the code below. I placed a little if/else isset loop at the bottom to see if the cookies were set and they are.

I suspect there is a problem with the $update_code variable. Not sure, but I need some help.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     require 'common.php';
  4.  
  5.  
  6.         //session_start();
  7.     session.auto_start;
  8.  
  9.  
  10.         $_SESSION['procFlag'] =1;
  11.  
  12.  
  13.  
  14.     $code = $_REQUEST['code'];
  15.     $num = $_REQUEST['num'];
  16.     $update_code= $_REQUEST['update_code'];
  17.  
  18.  
  19.  
  20.     if($_SESSION['procFlag'] ==1)
  21.         {
  22.  
  23.     // adds number of items ordered to the cart
  24.     if ($code) $_SESSION['cart'][$code] +=$num;
  25.         {
  26.     $_SESSION['ProcFlag']++;
  27.     }
  28.  
  29.  
  30.         //removes all items ordered from the cart
  31.     if ($update_code) $_SESSION['cart'][$update_code]--; 
  32.         }
  33.  
  34.  
  35.  
  36.  
  37. ?>
  38.  
  39. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  40. <head>
  41. <meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
  42. <title></title>
  43. <link rel="stylesheet" type="text/css" href="cart.css">
  44. </head>
  45. <body>
  46. <h1></h1>
  47. <div class="goods-list">
  48. <table>
  49. <tr><th align="left">Image<br /></th><th align="left">Product Name<br /></th><th align="left">Unit Cost<br /></th><th>Quantity<br /></th><th>Total<br /></th></tr>
  50.  
  51.  
  52. <?php
  53.  
  54. $sum = 0;
  55.  
  56.  
  57.       foreach ($_SESSION['cart'] as $code =>$num)
  58.           {
  59.           if($num <=0 ) {continue;}
  60.  
  61.             //selects goods from database
  62.         $sql = "SELECT * FROM goods WHERE code=" .$code;
  63.         $mysql->query($sql);
  64.                 $row = $mysql->fetch();
  65.  
  66.  
  67.  
  68.                // quantity ordered x unit price 
  69.         $ss = $num * $row['price'];
  70.  
  71.  
  72.         // calculates total cost of all goods
  73.         $sum += $ss;
  74.  
  75.  
  76.  
  77.  
  78.         //echo's the pic file in the database
  79.         echo "<tr><td>";
  80.                 $pic = $row['pic'];
  81.                 $host = get_host();
  82.  
  83.  
  84.  
  85.         if (strlen($pic)>0)
  86.         {
  87.     $disp_pic = "http://$host/img/" .$pic;
  88.     }
  89.          echo "<img src=\"$disp_pic\" border=\"0\" width=\"60px\" height=\"60px\"></td>";
  90.          echo "<td class=\"gname\">{$row['name']}</td>
  91.         <td class=\"gtanka\">{$row['price']}</td>
  92.         <td class=\"gsuuryou\">{$num}</td>
  93.         <td class=\"gsyoukei\">{$ss}</td>";
  94.  
  95.      echo '<td class=\"gsakujyo\" align=\"left\" ><form method="post" action="cart.php?<?=$code?>">';
  96.  
  97.  
  98.  
  99.      echo '<input type="hidden" name="update_code[]" value="' . $row['code'] . '">';
  100.  
  101.      echo ' <input type="submit" value="remove"></form></td></tr>';
  102.  
  103.  
  104.  
  105.          echo "<tr><td colspan='2'> </td>
  106.                    <td class=\"ggoukei_title\"></td>
  107.                    <td class=\"ggoukei\">{$sum}</td></tr>\n";
  108.  
  109.  
  110.  
  111.  
  112. }
  113.  
  114. ?>
  115.  
  116. <?php
  117.  
  118. if (isset($_SESSION['cart']))
  119. {
  120. $_SESSION['cart']=$row['name']++;
  121. }
  122. else
  123. {
  124. $_SESSION['cart']="cookies are not set";
  125. }
  126. echo $_SESSION['cart'];
  127. ?>
  128.  
  129.  
  130. </table>
  131. <br><a href="index2.php?code=<?=$row['code']?>">CONTINUE SHOPPING  </a>
  132. <br><a href="cart_empty.php">CHECKOUT  </a>
  133. </div>
  134. </body>
  135.  
  136.  
  137.  
  138. </html>
  139.  
Apr 2 '11 #1
0 1523

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Don Grover | last post by:
I am retrieving costs and product id's from a sql db. and need to build a shopping cart around it. How do I store the selected items and qty req so I can move into another catalog and total up as...
0
by: DoomSlayer | last post by:
Hi I'm looking for a Shopping Cart Solution that has/can do the following: 1. Ease of integration with existing site layout 2. User friendly admin interface (even for non-techies like my...
2
by: Paul Bruneau | last post by:
Hi, I hope someone can help me make a working shopping cart, as a learning tool. If I have a "Product Demo" html page with a "Buy Me" button, there must be a simple javascript method of...
1
by: madison | last post by:
Hi, I am trying to start a website using paypals shopping cart function. If i have 10 items and they sell out, how do I make it so the item is then listed as sold out. The next person would not...
17
by: Phil McKraken | last post by:
I am having a problem putting together a shopping cart with the below script. Everything displays fine, adds totals fine, and works perfect EXCEPT if you choose the 9.95 item #5 BY ITSELF the total...
2
by: G.E.M.P | last post by:
High Level Session Handling Design for a Shopping cart 0) What am I missing? 1) How does OSCommerce do it? I'm thinking about building a shopping cart from scratch, using a library of dynamic...
7
by: isaac2004 | last post by:
hi i have a basic asp page that acts as an online bookstore. on my cart page i am having trouble generating 3 numbers; a subtotal, a shipping total, and a final price. here is my code i would...
1
by: jecha | last post by:
I'm implementing a shopping cart but am having a problem in checking out a person who has added item in his/her shopping busket.The code for the checkout.php script is given below <?...
15
gregerly
by: gregerly | last post by:
Hello, I once again turn to this community of genius' for some help with a problem. I've got a shopping cart (which is working well so far) that I will be implementing all kinds of AJAX...
1
by: BenKen | last post by:
Hi all pls I'm having great difficulty in making my shopping cart work. I am a newbie in php with little understanding of it. Add to cart button doesn't work and shopping cart in general doesn't. Pls...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.