[英]update quantity in shopping cart php (update array value)
我并不想屈尊,只是要清楚。
所以! 我有一个包含数组的会话。 我有从URL使用GET ['id']设置的“ id”(例如:php?id = 1)。 我也有'qty',它自动为1。
当将几个id编号添加到会话中时,假设id => 1,id => 2,id => 3,当然,这三个数组的数量=> 1。
我想更新“ id”编号2的“ qty”值,而不更改其他ID编号(1和3)的“ qty”值。 我想为每个单独的ID执行此操作。
贝娄是我的代码,要运行它,请将其另存为cart.php。 然后运行三次,第一次是localhost / cart.php?= 1,localhost / cart.php?= 2,localhost / cart.php?= 3。 这样您就可以在会话中存储三个ID号。 然后转到cart.php。
index.php **代码
<div class="product">
<h3>Baketball</h3>
<a href="add-to-cart.php?id=1">Add to cart</a>
</div>
<div class="product">
<h3>Football</h3>
<a href="add-to-cart.php?id=2">Add to cart</a>
</div>
<div class="product">
<h3>Baseball</h3>
<a href="add-to-cart.php?id=3">Add to cart</a>
</div>
cart.php **代码
// set the array and store in session
$_SESSION['items'][]=array(
'id'=>"'".$_GET['id']."'",
'qty'=> 1
);
// Display each array value stored in the session
foreach ($_SESSION['items'] as $value) {
// Display the id
echo 'The ID is ' ."''''" . $value['id'] ."''''" ;
// Display the Qty
echo 'the QTY ' ."''''" . $value['qty'] ."''''" ;
echo "<br/>";
//Display and edit quantity in a form.
echo "<form method='post' action='id.php' name='edit'>
<p>Quantity: <input type=\"text\" size=\"20\" name='".$value['id']."' value='".$value['qty']."'/> </p><br>
<input class='save' name='update' value='update' type='submit'>
</form>";
}
// check if the update button is submited so that the qty value can be changed where the id number of the selected qty input box is edited
if (isset($_POST["update"])) {
$_SESSION['item'][$id]= $_POST["'".$value['id']."'"];
}
?>
在您发布的其余代码中,我看不到$id
位置。 因此,请确保在这里再次定义它。
将您的输入更改为此:
echo "<p>ID: <input type='text' name='id' value='" . $value['id'] . "'></p>";
echo "<p>Quantity: <input type='text' size='20' name='qty' value='" . $value['qty'] . "'/></p>";
理想情况下,id字段应为type =“ hidden”,但至少您可以看到要测试的数字。
然后,您可以使用它来更新会话:
$id = $_POST['id'];
$qty = $_POST['qty'];
$_SESSION['item'][$id] = $qty;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.