471,594 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 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 1086

Post your reply

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

Similar topics

2 posts views Thread by Don Grover | last post: by
2 posts views Thread by Paul Bruneau | last post: by
1 post views Thread by madison | last post: by
17 posts views Thread by Phil McKraken | last post: by
2 posts views Thread by G.E.M.P | last post: by
1 post views Thread by jecha | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.