Solved

shopping cart problem php

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP & MySql - Turn Select Query Results into a Glyphicon 3 28
Advice and best practice  SQLite3 Database using PHP 2 46
hasing a url 16 23
Insert values are dynamic 11 41
Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to dynamically set the form action using jQuery.

816 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

7 Experts available now in Live!

Get 1:1 Help Now