I have for each loop which when looping outputs the items held in a basket array. This works great, but I want to store a few things per EACH item in session variables so I can use this information elsewhere, I am trying to create session variables inside the foreach loop, but of course the variables need to have different names every time it loops through a different item. I have researched how to create variables dynamically, I couldn't figure it out. This is something I have always felt would come in handy, but as it seems difficult I just avoid it, but now I want to know if its possible.
Here is my for each loop code, you can see inside the three session variables I want to create for each item in the basket:
foreach ($basketarray as $value)
{
echo "<div id='basketitems'><br/>
".$value['name']."<br/>
".$value['id']."<br/>
£".$value['price']."<br/>
".$value['size']."<br/>
Quantity: ".$value['quantity']."<br/><br/>
<img id='searchimage' src=".$value['picture']." width='210' height='250' /><br/>";
$_SESSION['Bprodid'] = $value['id'];
$_SESSION['Bprodquantity'] = $value['quantity'];
$_SESSION['Bprodprice'] = $value['price'];
echo "<form action='deletefrombasket.php' name='basketdelete$items' id='basketdelete$items' method='POST'>
<input type='submit' name='".$value['basketid']."' value='Remove' id='basketid' name='basketid'/>
</form></div>";
$items++;
}
?>
<div id='basketdetails'>
<?php
echo "<p>items ". number_format($basketitems)."</p>";
echo "<p>Total £ ".number_format($baskettotal, 2, '.', ',')."</p>";
if($basketitems && $baskettotal !=0)
{
echo "<a href='clear.php'>Empty Basket</a>";
echo "<a href='checkout.php'>Checkout</a>";
}
So it is possible to do something like this? I was trying to create varaible names by using a counter but I was unsuccessful.
I would appreciate any advice.
Thanks