Solved

shopping cart problem php

Posted on 2004-09-14
3
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Are you using email marketing software? If not, you're missing out on effortless marketing and the reaching of desired conversion rates through email marketing software.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

732 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