Jump to content

mcChris

Member
  • Posts

    8
  • Joined

  • Last visited

Posts posted by mcChris

  1. here's the link code i'm using:

     

    Add to Cart

     

    it's very weird and hard to explain...the two browsers (ff & chrome) were seemingly refreshing the page when i clicked the link, thus adding another product in the page transition, giving me 1 product in the cart and 2 in the source code...

     

    ie6 doesn't do this...and i've been conditioned to believe if ie6 gets something right, it's really wrong...

     

    i also put the project code on my server, only changing the '<?=' to '<?php echo' and got the same weird browser behavior...and, i've been over the code several times and can't see an error anywhere...

     

    so i gave up and used the button...and it works...i don't know why...

  2. Update: my shopping cart is 'magical' :rolleyes: ...this situation is truly bizarre!

     

    after staring at this code for another day, i've noticed that when i click on the 'addToCart' link, the addToCart page displays 1 item in the quantity with the correct amount, shipping, and total; however, on viewing the source code i see this:

     

    >
    </pre>
    <table>Product IDQuantityAmountTULIPCHOC3$74.97Shipping:$32.97Total:$107.94</table>

     

    it's good math, but not a practical application :lol: ...thoughts?

  3. from ShoppingCart.php:

     

    <?php
    class ShoppingCart {
       protected $items = array();
    
       public function AddItem($product_id) {
           if(array_key_exists($product_id, $this->items)) {
               $this->items[$product_id] = $this->items[$product_id] + 1;
           }
           else {
               $this->items[$product_id] = 1;
           }
       }
    
       public function GetItemCost($product_id) {
           $cost_string = get_item_cost($product_id);
           $cost_float = "$cost_string" + 0;
    
           return $cost_float * $this->GetItemQuantity($product_id);
       }
    
       public function GetItemShippingCost($product_id) {
           return $this->GetItemQuantity($product_id) * $this->getShippingCostFor(get_item_cost($product_id));
       }
    
       private function getShippingCostFor($price) {
           if ($price < 10) {
               return 1.99;
           }
           elseif ($price < 50) {
               return 10.99;
           }
           else {
               return 34.99;
           }
       }
    
       public function GetItems() {
           return array_keys($this->items);
       }
    
       public function GetItemQuantity($product_id) {
           return intval($this->items[$product_id]);
       }
    
       public function EmptyCart() {
           $this->items = array();
       }
    }
    ?>
    

     

    and functions.php:

    <?php
    // DEFINE GLOBALS
    
    define ('STORE_XML_FILE', '../catalog.xml');
    
    // DEFINE REFERENCES
    
    require_once ('templates.php');
    
    require_once ('../classes/ShoppingCart.php');
    
    // FUNCTIONS
    session_start();
    
    function get_xml_catalog() {
       return new SimpleXMLElement(file_get_contents(STORE_XML_FILE));
    }
    
    function get_item_cost($product_id) {
       foreach(get_xml_catalog() as $product) {
           if ($product_id == $product->id) {
               return $product->price;
           }
       }
       throw new Exception('item not found: ' . $product_id);
    }
    
    function product_exists($product_id) {
       foreach(get_xml_catalog() as $product) {
           if ($product->id == $product_id)
               return true;
       }
       return false;
    }
    
    function get_shopping_cart() {
       if (!isset($_SESSION['cart'])){
           return new ShoppingCart();
       }
       else
           return unserialize($_SESSION['cart']);
    }
    
    function set_shopping_cart($cart) {
       $_SESSION['cart'] = serialize($cart);
    }
    ?>
    

  4. i'm working my way thru the shopping cart tutorial...in video 7, 12:13 minutes in...i've just completed the code for $quantity:

     

    ><?php
       $product_id = $_REQUEST['id'];
    
       if (product_exists($product_id)) {
           $shopping_cart->AddItem($product_id);
       }
    ?>
    
    </pre>
    <table>Product IDQuantityAmount
               $product_id
    
               $quantity
    
               $ amount
    </table>
    <br><br><?php set_shopping_cart($shopping_cart);<br><br

     

    in the video, when he refreshes the page, the item quantity goes up 1 as expected...when i refresh my page, the item quantity goes up by 2 each time...not good... :(

     

    i've compared my code with the code base and don't see an error...in my first attempt, i continued on with the videos, hoping the issue would resolve, but it didn't...so, i scrapped that code and started again from scratch, with the same result...checked the code again and i'm still not seeing an error...any thoughts as to what might be the cause?

×
×
  • Create New...