SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Changing quantities on last row does not update cart

    I am using an OO shopping cart modifed for use in lan-based invoicing system.
    My trouble is this: I have added some methods that will compute tax and add it into the total amount owed.

    I echo the contents of the cart like so:
    PHP Code:
           $result = $_SESSION['cart']->getItems();
           $i = 0;
           while ( @$prod = $result->fetchObject() ) {
             $qty = $_SESSION['cart']->items[$prod->id];
             $total = $prod->Price * $qty;
             $i++; ?>
           <input type="hidden" name="id[]" value="<?php echo $prod->id?>" />
           <div class="row">
              <div class="del">
              <a href="./invoice.php?func=remove&id=<?php echo $prod->id;
              
    ?>&tax=<?php echo $_SESSION['cart']->tax?>">DEL</a>
              </div>
              <div class="line"><?php echo $i?></div>
              <div class="qty"><input type="text" size="3" width="10px" name="qty[]"
                    value="<?php echo $qty?>" />
              <a href="javascript: frmsubmit('recalc');">Add</a></div>
              <div class="ref"><?php echo $prod->Reference?></div>
              <div class="desc"><?php echo $prod->Description?></div>
              <div class="price"><?php echo $prod->Price?></div>
              <div class="total"><?php print ( number_format $total2'.'',' ) ); ?>
              </div>
           </div><?php // end while() loop ?>
    Now, if there is just one row (meaning 1 item in the cart), the update links work as expected: the quanitities update properly, the price totals and tax amounts all update and all is peachy.

    If there is more than one row, then the last row to print does NOT update properly.

    If I enter a quantity for that item higher than 1, the quantity field shows the quantity you entered, but now the tax amount shows almost zero and then the grand total is off as well. Is the problem caused by the way I am looping through the cart items array?

    Here also are the methods I am using to compute the totals and the tax. Please, if there are any ways I can tighten up any of the code, please have at it But my main concern is getting any changes to the last item printed to update the cart totals and tax.
    PHP Code:
          function update_qty ( &$form )
         {
          foreach (
    $form["id"] as $i => $productid) {
            
    $qty $form["qty"][$i];
            
    $this->set($productid$qty);
           }
         }
          
          function 
    getTax $taxValue )
         {
           
    $this->tax 0;
            if ( isset ( 
    $taxValue ) && !empty ( $taxValue ) ) {
              
    $this->tax number_format $taxValue1'.'',' );
              } else {
              
    $this->tax number_format '5.9'1'.'',' );
              }
             return 
    $this->tax;
         }
         
         function 
    recalc_total()
         {
         
    $this->total 0;
         
    $in_clause $this->getIdList ();
        if ( empty ( 
    $in_clause ) ) {
           return 
    false;
          }
         
    $sql "SELECT " ID " AS id, " PRICE " FROM " TABLE "
             WHERE " 
    ID " IN ($in_clause)";
        
    $this->getDBConn();
        
    $result = &$this->db->query $sql );
         while ( 
    $product $result->fetchObject () ) {
          
    $this->total += $this->items[$product->id] * $product->Price;
           } 
    //end while
         
    $this->total number_format $this->total2'.'',' );
         return 
    $this->total;
         } 
    // end function recalc_total
        
         
    function calcTax ()
         {
          
    $this->taxAmount 0;
          
    $taxAmount $this->total 100 $this->tax;
          
    $this->taxAmount number_format $taxAmount2'.'',' );
          return 
    $this->taxAmount;
         }
         
         function 
    gtotal ( &$taxValue )
         {
          
    $this->grandtotal 0;
          
    $this->getTax $taxValue );
          
    $this->calcTax();
           if (isset(
    $this->total))  {
            
    $this->grandtotal $this->total $this->taxAmount;
            
    $this->grandtotal number_format $this->grandtotal2'.'',' );
             } 
    //end if
          
    return $this->grandtotal;
         }
         
         } 
    //end Class CART 
    Last edited by garlinto; Jan 4, 2005 at 09:36. Reason: Screwed up the title.
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  2. #2
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I've done it again. It seems that everytime I have been posting lately, I come up with the answer on my own eventually. I guess postin wy question helps me troubeshoot the problem a little better.

    It turns out that there is nothing wrong with my code. The problem was with php's number_format() function. I found that there was another individual that was having a similar problem with number_format coughing with numbers over 1,000.

    So I am going to have to use sprint() untill I can figure out while number_format is not working right. Has anyone else noticed this? Maybe its just my implimentation of it?

    Here's the linie that was creating the problem:
    PHP Code:
    $this->total number_format $this->total2'.'',' ); 
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  3. #3
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You just doing that to boost your posts?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •