0

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/>
     &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>";

$items++;
}
?>
    <div id='basketdetails'>
<?php

echo "<p>items ". number_format($basketitems)."</p>";
echo "<p>Total &pound; ".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

0

4 Answers 4

3

Put the values in arrays. Then use a counter that increments for each loop to sderver as the key for each value.

$counter = 0;
foreach ($basketarray as $value)
{
    // Code goes here

    $_SESSION['Bprodid'][$counter] = $value['id'];
    $_SESSION['Bprodquantity'][$counter]  = $value['quantity'];
    $_SESSION['Bprodprice']$counter[]  = $value['price'];

    // More code
    $counter++
}

To access each value use a for loop:

$size = count($_SESSION['Bprodid']);
for ($i = 0; $i < $size; $i++)
{
    echo $_SESSION['Bprodid'][$i] . "<br>\n";
    echo $_SESSION['Bprodquantity'][$i] . "<br>\n";
    echo $_SESSION['Bprodprice'][$i] . "<br><br>\n";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help I have spent some time trying your example but it is not working. How do I use the session variables stored in another script, normally it can be set to a variable and the variable used in the script, should this still work the same way? Thanks!
It should work the same way. To loop through the new session variables you would use a for loop. See my updated answer for an example.
Thanks for your updated code, I was trying it out but its not working. I echo out $_SESSION['Bprodid'][$counter] and it displays 13, which are the two id's from the two items in the basket, 1 and 3. But when I echo $size, it tells me the value is 1, because when I was running the for loop it only echo's 11 which is id 1 as the first item is id1 and 1 for the quantity. I may be using it wrong, I ill keep trying, thank you!
2

Your session variable can be a multi-dimensional array, so before the loop you can say:

$_SESSION['my_values'] = array();

And in the loop:

$_SESSION['my_values'][$value['id']]['Bprodquantity'] = $value['quantity'];
$_SESSION['my_values'][$value['id']]['price'] = $value['price'];
// etc.

Edit: To loop through all your values you can use:

foreach ($_SESSION['my_values'] as $key => $values)
{
  echo $key; // the ID of your product
  echo $values['quantity']; // the quantity
  echo $values['price']; // the price
}

3 Comments

Thanks for your input, I am testing your solution, but I was wondering how can I get this to hold more than one value? Say I use this $_SESSION['my_values'][$value['id']]['price'] = $value['price']; inside the loop, then outside I echo this by using echo $_SESSION['my_values'][$value['id']]['price'], it only echo's the last value. In my basket I have 3 items for testing and they have three prices needing stored, this only echo's the last price. I am wondering how to use your solution correctly, am I missing something? Many thanks!!
@deucalion0 The trick is [$value['id']]; in your $_SESSION['my_values'] you now have 3 values that you can access directly via their ID or you can loop through them using a foreach on the $_SESSION['my_values'] array. Every item you loop through is an array with the keys quantity and price (or whatever you add). I'll edit my answer.
This was genius, I was instantly able to use the session array in my insert action script and it added all items to the table as required! Many many thanks for helping me out, I spent a lot of time on this!!! Cheers!
1

Store you values into an array $products[$items]["prodId"] and so on and after the loop set your session variable to the array, $_SESSION["products"] = $products. You can then access the items with $_SESSION["products"][$itemNumber]["prodId"] and so forth.

Comments

1

You can use serialize() and unserialize() function to save whole array in session:

$_SESSION['sbasketarray'] = serialize($basketarray);

and in other script which need data:

if (isset ( $_SESSION['sbasketarray'] )) {
    $basketarray = unserialize  ( $_SESSION['sbasketarray'] );
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.