I need to return all items from a session. To make that happen I query the database with the following method:
public function get_cart_items($ids)
{
$sql = "SELECT id
, name
, price
FROM products
WHERE id IN (?)
ORDER BY name";
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array($ids));
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
In the Controller I use:
if(count($_SESSION['cart_items'])>0){
$ids = "";
foreach($_SESSION['cart_items'] as $id => $value){
$ids = $ids . $id . ",";
}
$ids = rtrim($ids, ',');
}
$heading = 'Producten in je cart';
$cart_items = $this->shop->get_cart_items($ids);
$pagina_vars = array (
'body_id' => 'shop',
'meta_tags' => $this->page->get_pagina_content($this->pageId)
);
$this->render('cart', compact('heading','cart_items') + $pagina_vars);
This is just returning one item from the cart, while it should return 3 (I have added a counter to the page to test)
if(isset($_SESSION["cart_items"]))
{
$cart_count = count($_SESSION['cart_items']);
}
else
{
$cart_count=0;
}
and that gives me indeed 3. So there must be something wrong in the Model and/or Controller. Only I don’t see what?
Thank you in advance