arrays - PHP: Foreach echo not showing anything -


edit

now can output current product, every time form adds item gets overriden. want make list incremental this:

 1. banana 3 units, price 350 crc 2. yougurt 4 units price 2000 crc 3. etc etc 4. etc 

the current output shows last added item.

this script:

<?php  session_start();  //getting list $list= $_session['list'];   //stock $products = array(        'pineaple' => 500, 'banana' => 50, 'mango' => 150,        'milk' => 500, 'coffe' => 1200, 'butter' => 300,       'bread' => 450, 'juice' => 780, 'peanuts' => 800,       'yogurt' => 450, 'beer' => 550, 'wine' => 2500,   );  //saving stuff $_session['list'] = array(     'item' => ($_post['product']),      'quantity' => ($_post['quantity']),     'code' => ($_post['code']), );  //price $price = $products[($_session['list']['item'])] * $_session['list']['quantity'];  $_session['list']['price'] = $price;   //listing echo  "<b>shoppign list</b></br>";  foreach($_session $key => $item)  {     echo $key[''], '. ', $item['item'], ' ', $item['quantity'], ' units: ', $item['price']; }  //recycling list  $_session['list'] = $list;  echo "</br> <a href='index.html'>return index</a> </br>";   //printing session var_dump($_session);  ?> 

this line issue.

$_session['list'] = array('price' => $price,); 

you're setting variable you're trying iterate across array single entry in it, not mention fact $price isn't going nested array, why trying item['key'] failing (as in 'price' key , $price item in foreach).

edit:

i believe, second quick glance you're intending this:

$_session['list']['price'] = $price; 

correct me if i'm wrong.

edit 2:

actually, looking again, i'm not quite sure understand structure $_session['list'] variable. looks want like:

(('item' => 'banana', 'quantity' => 1...), ('item' => 'apple', 'quantity' => 2...)) 

but have (from fact reference $_session['list']['item']) only:

('item' => 'banana', 'quantity' => 1...) 

you have multiple problems here. first try , deal bad structure of $_session['list'] try , deal foreach loop.

edit 3:

i still don't think you're quite understanding mean, i'm going fix code i'm pretty sure you're looking for...

i'm pretty sure you're going looks this:

<?php  session_start(); $products = array(       'pineaple' => 500, 'banana' => 50, 'mango' => 150,        'milk' => 500, 'coffe' => 1200, 'butter' => 300,       'bread' => 450, 'juice' => 780, 'peanuts' => 800,       'yogurt' => 450, 'beer' => 550, 'wine' => 2500, );  if(!array_key_exists('list', $_session)){   $_session['list'] = array(); }  $price = $products[$_post['product']] * $_post['quantity']; array_push($_session['list'],            array(              'item' => $_post['product'],               'quantity' => $_post['quantity'],              'code' => $_post['code'],              'price' => $price,           ));      echo  "<b>shopping list</b></br>";     foreach($_session['list'] $key => $item) {     echo $key+1, '. ', $item['item'], ' ', $item['quantity'], ' units: ', $item['price']; }  echo "</br> <a href='index.html'>return index</a> </br>";  ?> 

Comments