PHP:Foreach回声没有显示任何东西 [英] PHP: Foreach echo not showing anything

查看:145
本文介绍了PHP:Foreach回声没有显示任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我可以输出当前的产品,但每次表单添加另一个项目时,它都会被覆盖。

编辑

我想列出这样的增量:

  1.香蕉3单位,价格350 CRC 
2. Yougurt 4单位价格2000 CRC
3.等等
4. etc

当前输出只显示最后添加的项目。

这是脚本:

 <?php 

session_start();

//获取列表
$ list = $ _SESSION ['list'];


//股票
$产品=数组(

'Pineaple'=> 500,'Banana'=> 50,'芒果'=> 150,
'Milk'=> 500,'Coffe'=> 1200,'Butter'=> 300,
'Bread'=> 450,'Juice' '花生'=> 800,
'酸奶'=> 450,'Beer'=> 550,'Wine'=> 2500,
)。
$ b $ //保存这些东西
$ _SESSION ['list'] = array($ b $'item'=>($ _POST ['product']),
'quantity'=>($ _POST ['quantity']),
'code'=>($ _POST ['code']),
);

// price
$ price = $ products [($ _ SESSION ['list'] ['item'])] * $ _SESSION ['list'] ['quantity'];

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


//列出
回显< b> SHOPPIGN LIST< / b>< / br>;

foreach($ _ SESSION as $ key => $ item)
{
echo $ key [''],'。 ',$ item ['item'],'',$ item ['quantity'],'units:',$ item ['price'];
}

//回收清单
$ _SESSION ['list'] = $ list;

echo< / br>< a href ='index.html'>返回索引< / a>< / br>;


//打印会话
var_dump($ _ SESSION);

?>


解决方案

这一行是您的问题。 b
$ b

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

您正在设置要迭代的变量是一个具有单个条目的数组在这里,更不用说 $ price 不会是一个嵌套数组,这就是为什么要获得 item ['key '] 失败(如'price'将是您的钥匙, $ price 将会是你的foreach中的项目)。

编辑:

我相信,从第二个快速浏览

  $ _ SESSION ['list'] ['price'] = $ price; 

如果我错了,请纠正我的错误。

<实际上,再次看,我不太清楚我是否理解你的 $ _ SESSION ['list']的结构。 变量。它看起来像你想要的东西:

 (('item'=>'Banana','quantity'=> 1 ...),('item'=>'Apple','quantity'=> 2 ...))

但是你有什么(从你引用 $ _ SESSION ['list'] ['item'] >的事实只有:

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

这里你实际上有多个问题。首先尝试处理 $ _ SESSION ['list'] 的错误结构,然后尝试处理foreach循环。



编辑3:

我仍​​然不认为你理解我的意思,所以我只是要修复代码是我很确定你在找... ...

我很确定你要做什么看起来像这样:

 <?php 

session_start();
$ products = array(
'Pineaple'=> 500,'Banana'=> 50,'Mango'=> 150,
'Milk'=> 500, Coffe'=> 1200,'Butter'=> 300,
'Bread'=> 450,'Juice'=> 780,'Peanuts'=> 800,
'酸奶' => 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>购物清单< / b>< / br>;
foreach($ _ SESSION ['list'] as $ key => $ item){
echo $ key + 1,'。 ',$ item ['item'],'',$ item ['quantity'],'units:',$ item ['price'];
}

echo< / br>< a href ='index.html'>返回索引< / a>< / br>

?>


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['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>";

?>

这篇关于PHP:Foreach回声没有显示任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆