Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7851539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:12:29+00:00 2026-06-02T19:12:29+00:00

I have for each loop which when looping outputs the items held in a

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:12:30+00:00Added an answer on June 2, 2026 at 7:12 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listbox and I want to loop through each of the items
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
I have a page which displays my database information. Through each loop, there is
I have an .each loop in jQuery. I want to iterate the loop one
I have a for each loop like this $sn_count = 1; $prodfilter = ;
I have loop and each iteration of Hash looks like this: [1, {:clid=>1, :nvz=>4,
I have a loop created with each, check this example: $('.foo').each(function(i){ //do stuff });
I have this loop which splits a Boolean LinkedList by 8 bits and return
I am struggling to output this multi-array, I have tried looping through using a
i have a loop where a variable value will change in each loop and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.