SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Basket Totals Not Refreshing?

    Hi all, I know this is going to be a bit of a longshot as I think this is quite complex and maybe hard to understand. I'm creating a website for a lens-hire firm, on the basket, there is a drop down list for the available hire periods such as 3, 7, 14, 21 and 28 day hire. I'm using a bit of jQuery AJAX to update the basket on change of the dropdown list. The cart is updating but without any totals, prices, etc...the title is still there. However, if I am to refresh the page the new hire period is back with all of the correct prices and totals, not sure what I'm doing wrong here. Please see my below code (there's a lot of it).

    Basket Class
    PHP Code:
    <?php

    class ShoppingCart {
        
        protected 
    $items = array();
        
        public function 
    is_empty(){
            if(empty(
    $this->items)):
                return 
    true;
            else:
                return 
    false;
            endif;
        }
        
        
    //Method for adding an item to the cart...
        
    public function add_items($id$info){
            
            
    //Is it already in the cart?
            
    if(isset($this->items[$id])):
                
                
    //Call the update_item() method:
                
    $this->update_item($id$this->items[$id]['qty']+1);
            
            else:
                
                
    //Add the array of info...
                
    $this->items[$id] = $info;
                
                
    //Add the quantity...
                
    $this->items[$id]['qty'] = 1;
                
            endif;
        }
        
        public function 
    update_item($id$qty){
            
            
    //Delete if quantity equals 0...
            
    if($qty == 0):
                
    $this->delete_items($id);
            
            elseif((
    $qty 0) && ($qty != $this->items[$id]['qty'])):
                
                
    //Update the quantity...
                
    $this->items[$id]['qty'] = $qty;
                
            endif;
            
        }

        public function 
    update($period){

            foreach(
    $this->items as $id=>$value):
                
    $this->items[$id]['option'] = $period;
            endforeach;
        }
        
        
    //Method for deleteing an item...
        
    public function delete_items($id){
            
            
    //Confirm this isn't in the cart...
            
    if(isset($this->items[$id])):            //Remove the item...
                
    unset($this->items[$id]);
            endif;
            
        }
        
        public function 
    count_cart(){
            
    //print_r($this->items);
            
    $i count($this->items);
                return 
    $i;

        }
        
        public function 
    cart_total(){
            foreach(
    $this->items as $value):
                
    $total += $value['price']*$value['qty'];
            endforeach;
            return 
    $total;

        }
        
        public function 
    viewCart($edit){
        
        
    //If there are products in session array then display cart...
        
    if($this->count_cart()>0):
        echo 
    "<form action=\"$_SERVER[REQUEST_URI]\" method=\"post\">";
            echo 
    "<input type=\"hidden\" name=\"do\" value=\"update\" />";
            
        echo 
    "<table width=\"100%\" class=\"basket\" cellspacing=\"0\" cellpadding=\"5\">";
            
            
    $total 0;
            
    $cnt 0;
            foreach(
    $this->items as $id=>$value):
                
    $price mysql_query(sprintf("SELECT products_price, products_timeframe, products_price FROM pricing WHERE pricing_id = %s"$value['option']));
                    
    $cost mysql_fetch_assoc($price);
                echo 
    "<tr>";
                echo 
    "<td width=\"78\"><img src=\"/images/products/$value[image]\" alt=\"$value[name]\" width=\"78\" height=\"60\" /></td>";
                echo 
    "<td>".$value['name'];
                echo 
    '<br/><span class="smallPrice">Price based on a '.$cost['products_timeframe'].' Day Hire</span>';
                echo 
    "</td>"

                echo 
    "<td>&pound;".number_format($cost['products_price'],2)."</td>";
                if(
    $edit===1):
                echo 
    "<td><input type=\"text\" name=\"qty[$id]\" size=\"3\" value=\"$value[qty]\" class=\"basketQty\"/>
                </td>"
    ;
                else:
                echo 
    "<td>$value[qty]</td>";
                endif;
                echo 
    "<td><strong>&pound;".number_format($cost['products_price']*$value['qty'],2)."</strong></td>";
                echo 
    "</tr>";
             
            endforeach; 
            
        echo 
    "</table></form>";

        
    //If there are no products with a matching session_id, then display message...
        
    else:
            echo 
    "<h3>Your basket is empty!</h3>";
        endif;
        }
        
        function 
    row_color($cnt,$even,$odd) { 
            echo (
    $cnt%2) ? "<tr class=\"$odd\">" "<tr class=\"$even\">"
        }

        function 
    grand_total(){

            foreach(
    $this->items as $id=>$value):
                
    $price mysql_query(sprintf("SELECT products_price FROM pricing WHERE pricing_id = %s"$value['option']));
                    
    $cost mysql_fetch_assoc($price);

                    
    $multi[] = $cost['products_price']*$value['qty'];
            endforeach;

            return 
    number_format($total array_sum($multi),2);

        }
        
    }

    ?>
    Script that the AJAX Runs
    PHP Code:
    <?php
    session_start
    ();
    require(
    'shopping.cart.php');

    //Initalize the shopping cart...
    if(isset($_SESSION['cart'])):
        
    $bag unserialize($_SESSION['cart']);
    else:
        
    $bag = new ShoppingCart();
    endif;

    if(isset(
    $_POST['period'])):
        
    $bag->update($_POST['period']);
        
    $_SESSION['cart'] = serialize($bag);
        echo 
    $bag->viewCart(1);
    else:
        echo 
    'Unsure';
    endif;

    ?>
    Appreciate this maybe hard to understand, I know it's a bit of a longshot - I also need to tidy up by tableHeader class and return a variable instead of so many echos!

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    I see a class, I see a PHP page... but your problem sounds like a javascript issue rather than a PHP one... so we probably need to see the handler for your AJAX call (specifically the return)

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey dude, thanks for the reply and taking the time to have a mooch through my code. I'm on a different computer at the moment and can't access by code...gerrr...will post it up as soon as I get back home

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there, please see the following code for the basket page itself...

    PHP Code:
    <script>

        function ajaxUpdate(help){

            if(help !==""){
                jQuery.ajax({
                   type: "POST",
                   url: "/includes/ajax_basket_updates.php",
                   data: help,
                   success: function(data){
                       jQuery('#basketTable').html(data);
                   },
                   error: function(){
                       alert('PROBLEM!');
                   }
                });
            }

        }

        jQuery(document).ready(function() {
            //If user updates hire period...
            jQuery('#pricingSelect').change(function(){
                ajaxUpdate('period=13');
            });
        });
    </script>

    <div id="rightCol">
        <form id="basketForm" action="<?php echo $_SERVER['REQUEST_URI'?>" method="post">
        <img src="/images/assets/order-process.gif" alt="Order Progress" style="margin: 0px 0px 10px 0px;"/>
        <div class="goingOn">Items in your shopping basket...</div>

        <div id="basketTable">
            <?php
            $bag
    ->viewCart(1);
            
    ?>
        </div>
      
        <div class="goingOn spacer">How long do you want it...the longer you hire, the more you pay!</div>

        <select id="pricingSelect" name="pricingSelect">
            <option value="3">3 Day Hire</option>
            <option value="7">7 Day Hire</option>
            <option value="14">14 Day Hire</option>
            <option value="21">21 Day Hire</option>
            <option value="28">28 Day Hire</option>
        </select>
        
        <div class="goingOn spacer">Right, we're nearly there now...just pick the date your hire will begin...</div>
        <?php
        
    require('includes/calender.class.php');
        
    $calender = new Calender;
        
        for(
    $x 0$x 3$x++):
            
    $date date('n'strtotime("+$x month"));
            if(
    $x==2):
                echo 
    '<div class="calendarPos right">';
            else:
                echo 
    '<div class="calendarPos">';
            endif;
            echo 
    '<div class="dateSay">'.date('F'strtotime("+$x month")).'</div>';
            echo 
    $calender->draw_calender($datedate('Y'));
            echo 
    '</div>';
        endfor;
        
    ?>
        
        <div class="goingOn spacer">Just pick your desired delivery method and we're done!</div>
        
        <?php
        $delivery 
    mysql_query("SELECT * FROM shipping");
        
        
    $num_rows mysql_num_rows($delivery);
            while(
    $del mysql_fetch_assoc($delivery)):
                
    $i++;
                
    //Below code finds the last iteration of the loop to border can be removed...
                
    if($i == ($num_rows 0)):
                    
    $class "shippingSub shippingBorderRemoved";
                else:
                    
    $class "shippingSub";
                endif;
                
                echo 
    '<div class="'.$class.'">';
                    echo 
    '<input type="radio" name="delivery"/>';
                    echo 
    $del['shipping_title'];
                    if(
    $del['weekday_sensitive']=="1"):
                        echo 
    ' (Mon - Fri Only)';
                    endif;
                    echo 
    '<div class="shippingPrice">&pound;'.$del['shipping_cost'].'</div>';
                echo 
    '</div>';
                unset(
    $class);
            endwhile;
        
    ?>
        
        <div class="goingOn total">Total: &pound;<?php echo $bag->grand_total(); ?></div>
        </form>
    </div>
    So for some reason the new selected hire period isn't being displayed or are the prices in the AJAX request, though the product name is. I can't figure out why :s

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys, a quick update, managed to get it all sorted, it was something stupidly simple...I didn't include the connection to my database therefore the script was failing. Thanks to those that did try to figure this out, hope I haven't wasted to much of your time


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
  •