Welcome Guest, Not a member yet? Register   Sign In
Weird logic error
#4

[eluser]TheFuzzy0ne[/eluser]
It's not totally clear to me what's happening here either. However, I do have a few suggestions:

CodeIgniter has an excellent sessions library. Unless you have a specific reason for wanting to use PHP sessions, I'd highly recommend you use it.

Encapsulation: I'm not sure if this is the problem, but most of your if and for* statements are missing curly braces. It makes your code very hard to read, and as you've just shown, quite difficult to debug. To save you some time, I've done it for you. Please let me know if it makes a difference:
Code:
function check_inventory($id, $quantity)
{
    $Q = $this->db->get_where('products', array('id' => $id));
    $p = $Q->row();
    
    return array($p->title, ($p->quantity - $quantity));
}
    
//this is the main function that is called by the controllers and determines which other model functions to call.
function update_cart($id = false, $quantity = 1, $checkout = false)
{
    
    if ($id) //if adding an item from the shop
    {
        # I've remove the item_id variable as it's unnecessary.
        $cart = ($id && $quantity > 0) ? $this->add_to_cart($id, $quantity) : $this->delete_from_cart($id);
    }    
    else //if updating the cart from a cart form
    {
        $cart = $this->update_cart_post();
    }  
    
    if ($checkout) //don't do anything if we're just checking out, just pass the cart on
    {
        $cart = $_SESSION['cart'];
    }

    //updating the cart is done, now set the session variables
    $_SESSION['cart'] = $cart;
    $_SESSION['sub_total'] = $this->cart_sub_total(); //cart_sub_total() depends on $_SESSION['cart'] being up to date
        
    //set optional variables
    
    if (isset($_SESSION['shipping'])) //cart_total_price() depends on $_SESSION['cart'] being up to date
    {
        $_SESSION['total_price'] = $this->cart_total_price();
    }
    
    if (!count($cart)) //if theres nothing left in the cart, user has 0'ed out the quantities, cart must be deleted
    {
        unset(
                $_SESSION['cart'],
                $_SESSION['total_price'],
                $_SESSION['sub_total'],
                $_SESSION['fl'],
                $_SESSION['shipping']
            );
    }
}
    
function update_cart_post()
{
    $cart = $_SESSION['cart'];
    $_SESSION['ship_cost'] = $this->input->post('ship_cost', true);
    $_SESSION['shipping'] = $this->input->post('shipping_options', true);
    $_SESSION['fl'] = ($this->input->post('fl', true) == 'y') ? 'y' : 'n';
    
    $ids = $this->input->post('id', true);
    $quantities = $this->input->post('quantity', true);
    
    //combine items and quantities arrays together into a cart array and get sub_total price and sum up item quantities
    $i = 0;
    $sub_total = 0;
    $total_items = 0;
    
    foreach ($ids as $id)
    {
        $item = $this->products_model->get_product($id); //get item price from db
        $cart[$id]['price'] = $item['price'];
        $cart[$id]['id'] = $id;
        $cart[$id]['title'] = $item['title'];
        $cart[$id]['quantity'] = $quantities[$i];        
        $i++;
    }
    
    //check for 0 quantities and delete that item
    foreach ($ids as $id)
    {
        if ($cart[$id]['quantity'] == 0)
        {
            unset($cart[$id]);
        }
    }
    
    return $cart;
}


Messages In This Thread
Weird logic error - by El Forum - 03-07-2009, 12:54 PM
Weird logic error - by El Forum - 03-07-2009, 01:09 PM
Weird logic error - by El Forum - 03-07-2009, 01:18 PM
Weird logic error - by El Forum - 03-07-2009, 01:33 PM
Weird logic error - by El Forum - 03-07-2009, 06:29 PM
Weird logic error - by El Forum - 03-07-2009, 06:58 PM
Weird logic error - by El Forum - 03-07-2009, 07:46 PM
Weird logic error - by El Forum - 03-07-2009, 10:15 PM
Weird logic error - by El Forum - 03-07-2009, 10:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB