I want to display all session data but have some control over it. the problem is, I can’t know exactly what’s stored in the session as it’s data from a shopping cart.
for example, here one variable in my session:
item_name_1 : Productname
item_quantity_1: 5
each products has some variables with a number. If there were 2 different products in the cart the second one would be
item_number_2 and so on.
there is also a variable called itemcount which tells how many different products are in the cart.
how can I tell php, that all variables that begin with item_name_ should be displayed for example in individual divs after all other variables, like total price?
this is what I use currently to print the session data:
<?php
foreach ($_SESSION as $key=>$val)
echo $key. ": ".$val. "<br>";
?>
this is what I see:
currency: EUR
shipping: 10
grandTotal: 70.5
itemCount: 4
item_name_1: Tomato Suppe
item_quantity_1: 1
item_price_1: 3.5
item_options_1:
item_name_2: Lentils Suppe
item_quantity_2: 14
item_price_2: 3.5
item_options_2:
item_name_3: Chicken Suppe
item_quantity_3: 1
item_price_3: 4.5
item_options_3:
item_name_4: Green Leaves Suppe
item_quantity_4: 1
item_price_4: 3.5
item_options_4:
Obviously I want to make it easier to read.
I can use echo $_SESSION[‘shipping’] for shipping cost to put it wherever I want, but how do I address the products?
say I want every product to be displayed within div and /div for example?
sorry for noob question.
thank you
If you can not control the way the data is saved to the session you could write a simple helper function which retrieves the product data in a more usable format.
You should of course add proper validation to check if the data in the $_SESSION array is set before accessing it.
Use it as follows