Solved

shopping cart problem php

Posted on 2004-09-14
3
306 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

Industry Leaders: 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

Title # Comments Views Activity
Length of for loop to be dynamic 2 31
Multi line FPDF footer: 3 22
PHP SMTP authentication 6 23
curl parse data from site 20 36
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.
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

680 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