“如何修复”;为foreach()提供的参数无效;在PHP中
在HTML表中显示数组数据时,为foreach()提供的参数无效。请帮我做这个 下面是我得到的错误, 警告:第56行的/Applications/XAMPP/xamppfiles/htdocs/mf/view_cart.php中为foreach()提供的参数无效“如何修复”;为foreach()提供的参数无效;在PHP中,php,html,foreach,Php,Html,Foreach,在HTML表中显示数组数据时,为foreach()提供的参数无效。请帮我做这个 下面是我得到的错误, 警告:第56行的/Applications/XAMPP/xamppfiles/htdocs/mf/view_cart.php中为foreach()提供的参数无效 <?php $bill= 0; $sno = 1; foreach($_SESSION as $products){ //print_r($products); pr
<?php
$bill= 0;
$sno = 1;
foreach($_SESSION as $products){
//print_r($products);
print "<tr>";
echo "<form action='edit_cart.php' method='post'>";
if (!$products) $products = array();
foreach($products as $key =>$value){
var_dump($products);
if($key == 0){
echo "<input type='hidden' name='name$key' class='form-control' value='".$value."'>";
echo "<td>".$value."</td>";
} else if($key == 1){
$p = $value;
echo "<input type='hidden' name='name$key' class='form-control' value='".$value."'>";
echo "<td>".$value."</td>";
}else if($key == 2){
$q = $value;
print "<td><input type='number' name='name$key' class='form-control col-xl-4 text-center' min='1' value='".$value."'></td>";
$bill = ($p * $q);
echo "<td>".($bill)."</td>";
echo "<td><input type='submit' name='event' value='Update' class='btn btn-sm btn-warning'></td>";
echo "<td><input type='submit' name='event' value='Delete' class='btn btn-sm btn-danger'></td>";
}
}
echo "</form>";
print "</tr>";
}
print "</table>";
print "</div>";
?>
看来您使用$\u会话对象的目的不仅仅是产品,可能还有一些用户信息。我建议将您的所有产品移动到如下位置:
$\u SESSION['products']
,这通常意味着您为foreach循环的arg 1提供的参数(数组/对象)不是数组或对象。我将把参数放在循环中,这样你就可以看到传递给循环的是什么。哪一个是第56行?您可能希望修复HTML,缺少起始
,
中不允许使用
,用表单包围表格或将其放置在
中。另外,then End的冗余
不是您当前的版本,但是此代码的格式在将来很难调试。试试看,也不知道为什么要交换echo
s和print
<代码>var_dump($products)需要帮助您。先生添加了var_dump($products);输出pleae help meLine 56是foreach($key=>$value的产品)
var_dump($products); Output.
array(5) { [0]=> string(5) "Shirt" [1]=> string(4) "1000" [2]=> string(1) "2" [3]=> string(1) "1" [4]=> string(14) "coconut-1.jpeg" }
array(5) { [0]=> string(5) "Shirt" [1]=> string(4) "1000" [2]=> string(1) "2" [3]=> string(1) "1" [4]=> string(14) "coconut-1.jpeg" }
array(5) { [0]=> string(5) "Shirt" [1]=> string(4) "1000" [2]=> string(1) "2" [3]=> string(1) "1" [4]=> string(14) "coconut-1.jpeg" }
array(5) { [0]=> string(5) "Shirt" [1]=> string(4) "1000" [2]=> string(1) "2" [3]=> string(1) "1" [4]=> string(14) "coconut-1.jpeg" }
array(5) { [0]=> string(5) "Shirt" [1]=> string(4) "1000" [2]=> string(1) "2" [3]=> string(1) "1" [4]=> string(14) "coconut-1.jpeg" }
array(3) { [0]=> string(14) "All in One Set" [1]=> string(3) "140" [2]=> string(1) "4" }
array(3) { [0]=> string(14) "All in One Set" [1]=> string(3) "140" [2]=> string(1) "4" }
array(3) { [0]=> string(14) "All in One Set" [1]=> string(3) "140" [2]=> string(1) "4" }