Solved

shopping cart problem php

Posted on 2004-09-14
3
299 Views
Last Modified: 2013-11-29
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
Comment
Question by:cimmer
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 497 total points
ID: 12058013
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
 

Author Comment

by:cimmer
ID: 12090316
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12090945
;)

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often people are aiming at development of perfect Magento websites. Though, it is easier said than done. You know what’s much easier? To ruin everything. It can be done in seconds. Many of us experimented with design, tried to change some values dir…
E-commerce is quite a gambling world, and you should never entrust your business to a lucky chance. In order to outrun your competitors in a race to attract as many customers as possible, you need to have a well thought-out strategy under your belt.…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now