[eluser]diego.greyrobot[/eluser]
Hello all, I've been trying to think through this subtle logic error in a pair of if blocks.
I have this section of code:
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 adding an item from the shop
if ($id) {
$item_id = $id;
if ($quantity > 0) $cart = $this->add_to_cart($item_id, $quantity);
else $cart = $this->delete_from_cart($item_id);
}
//if updating the cart from a cart form
else { $cart = $this->update_cart_post(); }
//don't do anything if we're just checking out, just pass the cart on
if ($checkout) $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
//cart_total_price() depends on $_SESSION['cart'] being up to date
if (isset($_SESSION['shipping'])) $_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']);
unset($_SESSION['total_price']);
unset($_SESSION['sub_total']);
unset($_SESSION['fl']);
unset($_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) {
//get item price from db
$item = $this->products_model->get_product($id);
$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;
}
in this particular function:
Code:
function update_cart($id = false, $quantity = 1, $checkout = false) {
//if adding an item from the shop
if ($id) {
$item_id = $id;
if ($quantity > 0) $cart = $this->add_to_cart($item_id, $quantity);
else $cart = $this->delete_from_cart($item_id);
}
//if updating the cart from a cart form
else { $cart = $this->update_cart_post(); }
//don't do anything if we're just checking out, just pass the cart on
if ($checkout) $cart = $_SESSION['cart'];
//code .....
}
...which gets passed in an int id in this particular case from segment 3 in the URL, gives me this output when i try to echo the value of id inside the first if block...
Code:
if ($id) {
die("id: ".$id);
$item_id = $id;
if ($quantity > 0) $cart = $this->add_to_cart($item_id, $quantity);
else $cart = $this->delete_from_cart($item_id);
}
//output: "id: 41";
But if take the same die statement and put it in the else block it also runs...
Code:
//if updating the cart from a cart form
else ($id === false) { die("id: ".$id); $cart = $this->update_cart_post(); }
//outputs: "id: "
I don't understand why the else block runs at all, or why the value of id is a bool(false) (if i do var_dump)
One thing i do know is that this code worked perfectly fine on my own pc during development, only after i moved it to the live server did this error show up. I checked that it was the same version, php5. Any ideas? what else should i check for?