Display Session Variables

I want to display all the session data, but have some control over it. the problem is that I can’t know exactly what is stored in the session as data from the shopping cart.

for example, here is one variable in my session:

item_name_1: Product Name
item_quantity_1: 5

Each product has some variables with a number. If there were 2 different products in the basket, then the second would be

item_number_2 etc.

There is also a variable called itemcount that tells you how many different products are in the basket.

how can I tell php that all variables starting with item_name_ should appear, for example, in separate divs after all other variables, such as the total price?

this is what i am currently using to print session data:

<?php foreach ($_SESSION as $key=>$val) echo $key. ": ".$val. "<br>"; ?> 

here is what i see:

Currency: EUR
delivery: 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" | item_quantity_4: 1
item_price_4: 3.5
item_options_4:

Obviously, I want to make reading easier.

I can use echo $ _SESSION ['shipping'] for the shipping cost to put it where I want, but how can I access the products?

Say I want each product to appear in a div and / div, for example?

Sorry for the noob question.

Thank you

+4
source share
4 answers

If you cannot control how the data is stored in the session, you can write a simple helper function that retrieves the product data in a more user-friendly 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 must, of course, add the correct check to check if the data is set in the $ _SESSION array before accessing it.

Use it as follows

 $products = getSessionProducts(); foreach($products as $product) { echo $product['name']; } 
+2
source

you can use something as rude 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 is done as you want. This is just an idea.

By the way, think what others are saying. If you control session vars, it’s best to keep them more organized. This obscene example will work with what you provided, though.
0
source

Whenever you start asking yourself: "How to handle variable variable names?" this is a good sign that you are heading in the wrong direction. As you saw, it’s hard and unnatural to look for $_SESSION['item_number_$n'] . It is easy, however, to iterate over the contents of an array, which, fortunately, you are allowed to store in a session (which, in addition to being stored between page loads, is just a normal array).

That is, instead of trying to figure out how to get to $_SESSION['item_number_5'] , you just watch $_SESSION['shopping_cart'][5] . This allows you to easily iterate over elements, count the number of elements, add elements, delete elements and just be cleaner to work with all things.

0
source

In any case, you should not use session variables to store such information. You must store the identifier to refer to the cart in the database in the session, and not store all cart information directly in the session variables.

0
source

Source: https://habr.com/ru/post/1437893/


All Articles