Is it possible to create variables dynamically in a for each loop?

I want to store data on items in my basket in session variables. The basket items are being displayed on the screen using the for each loop, and I have been trying to create session variables to hold specific data. Here is my code:

foreach ($basketarray as $value)
                {
        echo "<div id='basketitems'><br/>
        ".$value['name']."<br/>
        ".$value['id']."<br/>
         &pound;".$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>";

You can see that I am needing to create a session variable to hold and store id, quantity and price but for each item in the basket, this can be one or many. Is there a way to create these variables every time another item is run through the for each loop? I need to add the id, quantity and price for each item to an orders table individually as each item gets its own id in the table.

Any help is appreciated!

Thank you!

Hm, dont quite understand what you need, but you can create variable with something like this:

$variablename = "something";
$$variablename = "content of new variable";
echo $something;

output will be: content of new variable.

In your case why not just

$_SESSION['Bprodid'.$value['id']] = $value['id'];

of course you must know what $value[‘id’] is to access this variable afterwards.
Or you can simply create counter in your loop and


$_SESSION['Bprodid'.$i] = $value['id'];
$i++;

more on this - you could add this variables to an array so it will be more convenient to use.

Hi there boatmark I really appreciate your help! I tried your examples I couldn’t get any of them to work :frowning:

I’ll explain more clearly what I need to do.

So my for each loop echo’s out all of the items in the basket displaying stuff like id, name, price and a photo. I have three items in the basket just now for testing, on the screen appears these three items as echoed in the for each loop. Now I need to store the value of the id, price and quantity of each item that was displayed on the screen. So in another php file I will add this information to an orders table. To get the information I need to retrieve it from session variables, as this seems the easiest way to pass values in variables across the application.

So inside my for each loop, I have three session variables to store the three values I need, but of course these variables only hold data from the last item in the basket, the last to be iterated. So three items means I need 9 session variables, or some other way to add this data to the orders table.

The orders table has four columns, itemID, orderID, productID, quantity, price.

So for each item in the basket they need to be added to the table, one row per item.

This has been really tricky, I don’t know the best way to do this, my initial idea is seeming to be a bad one.

I am open to any suggestions.

Many thanks for any help!

quick example:


<?php
$item1['id'] = 1;
$item2['id'] = 2;
$item3['id'] = 3;
$basketarray = array(
        '1' => $item1,
        '2' => $item2,
        '3' => $item3,
);
foreach ($basketarray as $value) {
        $_SESSION['Bprodid'.$value['id']] = $value['id'];
}
echo $_SESSION['Bprodid1'];
echo $_SESSION['Bprodid2'];
echo $_SESSION['Bprodid3'];
?>

you will get “123” as a result.

as you can see with $_SESSION[‘Bprodid’.$value[‘id’]] you create new array element in array $_SESSION with name Bprodid + $value[‘id’], so if $value[‘id’] = 1 element name will be Bprodid1, and you can access it with $_SESSION[‘Bprodid1’];

Hope it will help.