Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

shopping cart problem php

I have built a shopping cart. The code items are down below. My problem is that from the shopping cart view page, I try to "update the quantity" and it doesnt "save" the new quantity I entered. Adding "new" items to the cart works fine but when I set the cart item to 0 (to remove) or to another number, it does not "save" the new item. Why??

Here is the code...



//add new item to cart and/or create cart
if($new)
{
      //create cart, if not created yet
      if(!session_is_registered("cart")){
            //cart
            $cart= array();
            session_register("cart");
            //#of items
            $items = 0;
            session_register("items");
            //total price
            $total_price="0.00";
            session_register("total_price");
      }
      
      //add item to cart
      if($cart[$new]){
            if($add){ $cart[$new]=$cart[$new]+$add;}
            else{$cart[$new]++;}
      
      }else{
            $cart[$new]=$cart[$new]+$add;
      }
}



//make changes to quantity of item already in cart
if($save){
      
      if (is_array($cart)) {

            foreach($cart as $prod_id => $qty){
                  if($$prod_id=="0"){
                        unset($cart[$prod_id]);
                  }else{
                        $cart[$prod_id] = $$prod_id;
                  }
            }
      }
      
}

 

//this function displays the cart on the screen to the user
//there are input boxes to change quantity and
//there is a "SAVE" button at the bottom to "save" changes to cart quantity.

function display_cart($cart)
{
  // display items in shopping cart


      echo '<h3>Shopping Cart Summary:</h3>';
  global $items;
  global $total_price;
      ?>
      <table><tr><td  style="width:100%;">
            <p><b>Items Currently in Your Shopping Cart</b><br>
            To change the desired quantity,<br> change the quantity field and click Update.<br><b>To remove an item from your
            cart</b>,<br> change the quantity to 0 and click Update.
            </p>
      </td>
      <td nowrap="nowrap"></td></tr></table>
 
  <form method="post" id="Form1">
      <table border="0" cellpadding="2" cellspacing="1" width="98%" bgcolor="#ADADAE">
        <tr>
          <th width="1%" bgcolor="#003399"></th>
          <th width="57%" bgcolor="#003399"><p><font color="#ffffff"><b>Product ID / Title</b></font></p></th>
          <th width="14%" bgcolor="#003399" align="right"><p><font color="#ffffff"><b>Price</b></font></p></th>
          <th width="13%" bgcolor="#003399" align="right"><p><font color="#ffffff"><b>Quantity</b></font></p></th>
          <th width="15%" bgcolor="#003399" align="right"><p><font color="#ffffff"><b>Subtotal</b></font></p></th>
        </tr>
      <?
 
 

      //display each item as a table row
      foreach ($cart as $prod_id => $qty)
      {
            //this variable removes sample from cart item if applicable
            $idnosample=$prod_id;
            ?>
            <tr>
            <td width="1%" bgcolor="#FFFFFF"></td>
            <td width="57%" bgcolor="#FFFFFF">
            <p>product id: <?
            echo  $idnosample ;
            
             ?><br><a href="item.php?pid=<? echo $idnosample; ?>" class="leftnav"><b><? echo get_product_name( $idnosample ) ?></b></a>
            </p></td>
            <td width="14%" bgcolor="#FFFFFF" align="right"><p>$<?
            echo number_format(get_product_price( $idnosample ),2) ;
            ?></p></td>
            <td width="13%" bgcolor="#FFFFFF" align="right"><input align="right" name="<? echo $prod_id ?>" value="<? echo $qty ?>" size="3" style="text-align: right;"></td>
            <td width="15%" bgcolor="#FFFFFF" align="right"><p>$<?
            echo number_format(get_product_price( $idnosample )*$qty,2) ;
            ?></p></td>
            </tr>      
            <?
      } //for each
 
  // display total row ?>
  <tr>
    <td colspan="5" bgcolor="#FFFFFF">
    </td>
  </tr>
  <tr>
    <td width="72%" colspan="3" bgcolor="#FFFFFF">
      </td>
    <td width="13%" bgcolor="#FFFFFF"><p><b>Total:</b></p></td>
    <td width="15%" bgcolor="#FFFFFF" align="right"><p><b>$<? echo number_format($total_price, 2) ?></b></p></td>
  </tr>
       </table>
      <table width="98%">
      <tr>
      <td align="right">
      <input type="hidden" name="save" value="true" />
            
      <input type="image" src="images/cart_Save.gif"   style="border:0px;"  name="Save" value="Save">
      <a href="https://website.com/checkout.php"><img src="images/cart_checkout.gif"  style="border:0px;"></a>
      </td>
      </tr>
      </table>
      </form>
      <?
 
}
0
cimmer
Asked:
cimmer
  • 2
1 Solution
 
Diablo84Commented:
Try replacing:

if($save){
     
     if (is_array($cart)) {

          foreach($cart as $prod_id => $qty){
               if($$prod_id=="0"){
                    unset($cart[$prod_id]);
               }else{
                    $cart[$prod_id] = $$prod_id;
               }
          }
     }
     
}

With:

if($save){    
     if (is_array($cart)) {
            for($i=0;$i<count($cart);$i++) {
             if ($cart[$i] == 0) unset($cart[$i]);
            }
     }  
}
0
 
cimmerAuthor Commented:
well, i figured out the problem i forgot to start the session before updating the cart... oh well im generous. Diablo gets the points... =)
0
 
Diablo84Commented:
;)

Always the simple things that cause the biggest problems isnt it, i remember something similar happening to me once with sessions and i couldn't see the problem, came back to it in the morning and noticed it straight away.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now