Afternoon,
I am having some difficulties with my shoppingcart for a webshop. In my cartArr (array that has the id's of my products got from my database) there are the right id's of my products that i bought. I also have a shoppingcart on my productspage that has the counts the right amount of items in my cartArr (so far so good).
When i want to show my products he only shows the last product i've clicked * times i've bought something. Logical because he doesn't remember it in my session array.
I know what my problem is but i can't figure out how to fix it.
/*Shoppingcart.php*/
<?php
$id = '';
if(isset($_POST['shoppingCart']['btnSubmit']))
{
$id = $_POST['shoppingCart']['id'];
$productDAO = new ProductDAO();
$result = $productDAO->getProductById($id);
if( in_array($id,$_SESSION['cartArr']))
{
array_push($_SESSION['cartArr'], $id);
echo '<pre>';
print_r('Hallo');
echo '</pre>';
}
}
$smarty -> assign('arrProducts',$result);
$smarty -> assign('id',$id);
$smarty -> assign('cartArr',$_SESSION['cartArr']);
$smarty -> assign('product',$result)
/*Shoppingcart.tpl*/
{foreach $cartArr as $item}
<div class="winkelkarItem">
<img src="{$arrProducts.image}" class="winkelkarImgPosition"
alt="chimay" title="chimay"/>
<ul class="winkelkarBeschrijving">
<li>{$arrProducts.naam}</li>
<li>{$arrProducts.prijs}€</li>
<li><input class="winkelkarAmmount" type="text" value="2"/></li>
<li>7.08€</li>
</ul>
</div>
{/foreach}
/*SQL statement to get my products out of my database in a seperate file ProductDAO*/
public function getProductById($id)
{
try
{
$sql = 'SELECT *
FROM tblProduct
WHERE id = :id';
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':id',$id);
$stmt->execute();
$result = $stmt->fetch();
}
catch(PDOException $e)
{
echo $e->getMessage();
}
return $result;
}
?>
Thx in advance for all help.
Short: Arrproducts has to get into my session Trough a foreach i need to make my items @ shoppingcart.tpl The id of the products i've clicked is in cartArr.
Thx!
In a first time,
If
$id
is in$_SESSION['cartArr']
, why do you push it another time ?In a second time, your last inserted item in your "shoppingCart" is
$result
(array fetched) and your "shoppingCart" is in array of ids stored in session.So, in your template Shoppingcart.tpl, when you loop on
$cartArr
,$item
is the id of your "product", not a variable who contains data of your "product". Then, in yourforeach
loop, you put$arrProducts.attribute
, but$arrProducts
corresponds to your last instert item.So you have to get the data of each id passed in the loop :
(I'm not sure of the smarty template syntax, but try to understand what I mean)
EDIT :
Another solution, construct another array whith data of each "product", and pass it in you tpl.
In "Shoppingcart.php" :
In you "Shoppingcart.tpl" :