【发布时间】:2020-09-14 07:01:43
【问题描述】:
我有两个数组,一个包含名为 aantal 的数量,另一个包含名为 producten 的产品。
产品:
Array
(
[0] => Array
(
[0] => 2
[1] => Tegel zwart
[2] => Zwarte tegel 2x2m
[3] => 47,5
[4] => 25
)
[1] => Array
(
[0] => 4
[1] => Tegel lichtgrijs
[2] => Lichtgrijze tegel 2x2m
[3] => 40,5
[4] => 25
)
[2] => Array
(
[0] => 2
[1] => Tegel zwart
[2] => Zwarte tegel 2x2m
[3] => 47,5
[4] => 25
)
[3] => Array
(
[0] => 4
[1] => Tegel lichtgrijs
[2] => Lichtgrijze tegel 2x2m
[3] => 40,5
[4] => 25
)
)
安塔尔:
Array
(
[0] => 20
[1] => 20
[2] => 27
[3] => 25
)
我想根据 url 中的 $_GET 值使用以下代码更新每个数量:
$sum = 0;
foreach($_SESSION['producten'] as $key => $product){
//$_SESSION['producten'][$key][4] = '';
$number = str_replace(',', '.', $product[3]);
$sum+= $number;
if(!empty($_GET['aantal'])){
foreach($_GET['aantal'] as $keyaantal => $aantal){
$_SESSION['producten'][$key][4] = $_GET['aantal'][$keyaantal];
}
}else{
$_SESSION['producten'][$key][4] = '1';
}
}
这是带有 html 的表单:
<form action="cart.php">
<?php foreach ( $_SESSION['producten'] as $row){
if(!empty($_GET['aantal'])){
$aantalwaarde = $row[4];
}else{
$aantalwaarde = 1;
}
// if($row != ){
// }
?>
<tr>
<td><?php echo $row[0] // ID; ?></td>
<td><?php echo $row[1] // Product_naam; ?></td>
<td><?php echo $row[2] // Beschrijving; ?></td>
<td><input name="aantal[]" type="number" value="<?PHP echo $aantalwaarde; ?>"></td>
<td>€<?php echo $row[3] // Prijs; ?></td>
<?php $total = $total + intval($row[3]); ?>
</tr>
<?php } ?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><input type="submit" value="Updaten"></td>
<td></td>
<td>Totaalprijs</td>
<td></td>
<td>€<?php echo $sum // Prijs; ?></td>
</tr>
</form>
为什么如果我单击更新按钮,它会更新所有产品的最后插入值?例如,如果我将最后输入的数量设置为 25,将之前的数量设置为 10,则所有产品的数量都为 25。
我该如何解决这个问题?
【问题讨论】:
-
如前所述,您忽略了这些是数组的事实。奇怪的事情可能会在一个循环中发生。在关键点添加一些调试代码以检查您正在使用的值,它们通常不是您认为您正在使用的值。