PHP: Foreach echo not showing anything
EDIT
Now I can output the current product, but every time the form adds another item it gets overriden. I want to make the list incremental like this:
1. Banana 3 Units, Price 350 CRC
2. Yougurt 4 Units Price 2000 CRC
3. etc etc
4. etc
The current output only shows the last added item.
This is the script:
<?php
session_start();
//Getting the 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 the stuff
$_SESSION['list'] = array(
'item' => ($_POST['product']),
'quantity' => ($_POST['quantity']),
'code' => ($_POST['code']),
);
//price
$price = $products[($_SESSION['list']['item'])] * $_SESSION['开发者_C百科list']['quantity'];
$_SESSION['list']['price'] = $price;
//listing
echo "<b>SHOPPIGN LIST</b></br>";
foreach($_SESSION as $key => $item)
{
echo $key[''], '. ', $item['item'], ' ', $item['quantity'], ' units: ', $item['price'];
}
//Recycling list
$_SESSION['list'] = $list;
echo "</br> <a href='index.html'>Return to index</a> </br>";
//Printing session
var_dump($_SESSION);
?>
This line is your issue.
$_SESSION['list'] = array('price' => $price,);
You're setting the variable you're trying to iterate across to be an array with a single entry in it, not to mention the fact that $price
isn't going to be a nested array, which is why trying to get item['key']
is failing (as in 'price'
will be your key and $price
will be your item in your foreach).
EDIT:
I believe, from a second quick glance you're actually intending to do this:
$_SESSION['list']['price'] = $price;
correct me if I'm wrong.
EDIT 2:
Actually, looking again, I'm not quite sure I understand your structure for your $_SESSION['list']
variable. It looks like you want something like:
(('item' => 'Banana', 'quantity' => 1...), ('item' => 'Apple', 'quantity' => 2...))
but what you have (from the fact you reference $_SESSION['list']['item']
) is only:
('item' => 'Banana', 'quantity' => 1...)
you actually have multiple problems here. First try and deal with the bad structure of $_SESSION['list']
then try and deal with the foreach loop.
EDIT 3:
I still don't think you're quite understanding what I mean, so I'm just going to fix the code to be what I'm pretty sure you're looking for...
I'm pretty sure what you're going for looks something like 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'] as $key => $item) {
echo $key+1, '. ', $item['item'], ' ', $item['quantity'], ' units: ', $item['price'];
}
echo "</br> <a href='index.html'>Return to index</a> </br>";
?>
You're overriding your $_SESSION['list'] value with just the calculated price, so when you iterate through $_SESSION['list'], the only thing you have in $item is a scalar value of the calculated subtotal, not the array you hope for under your 'saving the stuff' comment.
If you're simply trying to print out the array of product/quantity/code/price this should work:
<?php
session_start() ;
//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,
);
$_POST['product'] = 'Banana' ;
$_POST['quantity'] = 50 ;
$_POST['code'] = "unknown" ;
//Saving the stuff
$_SESSION['list'] = array(
'item' => $_POST['product'],
'quantity' => $_POST['quantity'],
'code' => $_POST['code'],
'price' => $products[$_POST['product']] * $_POST['quantity'],
);
print_r($_SESSION['list']) ;
//listing
echo "<b>SHOPPING LIST</b></br>\n";
foreach($_SESSION as $key => $item) {
echo $key, '. ', $item['item'], ' ', $item['quantity'], ' units ', $item['price']."\n";
}
echo "</br> <a href='index.html'>Return to index</a> </br>";
//Recycling list
$_SESSION['list'] = '';
//Printing session
print_r($_SESSION);
?>
That will print this result:
list. Banana 50 units 2500
精彩评论