Reputation: 7092
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
Upvotes: 4
Views: 17652
Reputation: 130
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.
function getSessionProducts()
{
$itemCount = $_SESSION['itemCount'];
$products = array();
for($i = 1; $i <= $itemCount; $i++) {
$products[] = array(
'name' => $_SESSION['item_name_'.$i],
'quantity' => $_SESSION['item_quantity_'.$i],
'price' => $_SESSION['item_price_'.$i],
'options' => $_SESSION['item_options_'.$i]
);
}
return $products;
}
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
$products = getSessionProducts();
foreach($products as $product) {
echo $product['name'];
}
Upvotes: 2
Reputation: 4301
You shouldn't really be using session variables to store this kind of information anyway. You should be storing an id to reference a shopping cart within a database in the session rather than storing all of the information about the cart directly in session variables.
Upvotes: 0
Reputation: 6507
Whenever you start asking yourself "how do I handle variable variable names?" it's a good sign you're heading in the wrong direction. As you've seen, it's difficult and unnatural to look for $_SESSION['item_number_$n']
. What is easy, however, is looping through the contents of an array which, fortunately, you're allowed to store in the session (which, other than how it persists between page loads, it just a normal array).
That is to say that, rather than trying to figure out how to get to $_SESSION['item_number_5']
, you just look at $_SESSION['shopping_cart'][5]
. This makes it easy to iterate through items, count the number of items, add items, remove items and will just be generally cleaner to work with all across the board.
Upvotes: 0
Reputation: 18833
you could use something as crude as this:
foreach($_SESSION AS $key => $val)
if (preg_match("/item_name/i", $key)) {
echo $val.'<br>';
}
}
Obviously you handle what you do if the match succeeds however you want. This is just to give an idea.
By the way, consider what others are saying. If you are in control of the session vars then it is a better idea to store them in a better, more organized way. This crummy example would work with what you provided though.
Upvotes: 0