mardi 19 mai 2015

Undefined Offset in if inside foreach

Trying to remove stock quantity from db... and if i have in my cart class, that stores in the session the product, it doesn't throw any errors but if i don't have the product that are in the db it throws Undefined offset: 10 in E:\w ... 10 representing the id from the products table for every product that i don't have in the cart but is in the table products this error is thrown

THE ERROR is on the line: if($product['id'] == $data['products'][$product['id']])

This is my function:

public function removeQuantity($data) {

    $getProds = $this->getProducts();
    $left = array();
    foreach($getProds as $product){
        if($product['id'] == $data['products'][$product['id']]) {
            $left[] = array(
            'quantity' => $product['quantity'] - $data['products'][$product['id']]['quantity'],
            'id' => $product['id'] );
        }         
    }
    $query = "";
    foreach($left as $times){
        $query .= "UPDATE products SET quantity = ? WHERE id = ?; ";
    }
    $exec = new Orders();
    $exec = $exec->flatten_array($left);

    $prod = $this->dbHandler->prepare($query);
    $prod->execute($exec);

}

HOW THE ARRAYS LOOK:

id from the array represent the id number of a product in the db

$product = array('id' => array( 'price' => 1, 'quantity' => 2 ....), ... );

$data = array('products' => array( 'id' => array('name' => 'car', ...), ...), ...);

$data is the session so it has couple of arrays in it...

Aucun commentaire:

Enregistrer un commentaire