0

I populate the $_SESSION['products'] array from a file:

$myFile = '.\products.txt';
$handle = fopen($myFile, 'r');

while (!feof($handle))
{
    $prod = explode('|', fgets($handle));

    $_SESSION['products'] = array($prod[4] => array(
            'name' => $prod[0],
            'price' => $prod[1],
            'description' => $prod[2],
            'image' => $prod[3]));
}

Then I want to loop through it, printing all the names and prices:

foreach ($_SESSION['products'] as $prodID=>$value) {
  echo $_SESSION['products'][$value]['name'];
  echo $_SESSION['products'][$value]['price'];
}

But it doesn't seem to work!

2
  • what is the output of print_r($_SESSION['products'])?? Commented Oct 10, 2012 at 7:49
  • Array ( [ylbulb] => Array ( [name] => 60W Yellow Bulb [price] => 7.95 [description] => Yellow Bulb description... [image] => ylbulb.png ) ) So all items are not being loaded into the array! Commented Oct 10, 2012 at 7:54

3 Answers 3

4

You need to check again how the foreach works. In your case, you can simply do the following:

foreach($_SESSION['products'] as $value) {
    echo $value['name'];
    echo $value['price'];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But it is only printing the last item. For some reason, all items in the text file is not being loaded into the $_SESSION['products'] array!
See MrCode's answer for that.
3

Your problem is, here you are overwriting the products array with every product, which means there will only ever be the last product present:

    $_SESSION['products'] = array($prod[4] => array(

Try appending to the array like:

$_SESSION['products'][$prod[4]] = array(
//                    ^^^^^^^^ set the key as the product ID here
        'name' => $prod[0],
        'price' => $prod[1],
        'description' => $prod[2],
        'image' => $prod[3]);

Also your foreach is wrong, try

foreach ($_SESSION['products'] as $prodID=>$value) {
  echo $value['name'];
  echo $value['price'];
}

7 Comments

Thanks but this isn't working. It giving an undefined index error and seems to multiplying the index with every page refresh.
Of course it will duplicate the array with a page refresh if you are calling this code again and again. $_SESSION data persists across pages, you should only populate the array from the file once.
ok thanks! i figured that out... however i still cant get to print anything due to the undefined index error
the two echos between the foreach loop
If the foreach is on a different page, be sure to call session_start(); before trying to use any session data. If you don't call that before setting or getting session data, it will not be persisted.
|
0
foreach ($_SESSION['products'] as $prodID=>$value) {
    echo $_SESSION['products'][$prodID]['name'];
    echo $_SESSION['products'][$prodID]['price'];
}

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.