1 回答
TA贡献2003条经验 获得超2个赞
<?php
if (!isset($_SESSION['basket'])) {
$_SESSION['basket'] = array();
}
if (isset($_POST['id'])) {
$_SESSION['basket'][$_POST['id']] = array(
'product_id'=>$_POST['id'],
'product_photo'=>$_POST['hidden_photo'],
'product_photo_alt'=>$_POST['hidden_photo_alt'],
'product_name'=>$_POST['hidden_name'],
'product_price'=>$_POST['hidden_price'],
'product_quantity'=>$_POST['quantity'],
);
}
// Update quantity
if (isset($_POST['action']) && $_POST['action'] == 'update') {
foreach($_SESSION['basket'] as &$value) {
if($value['product_id'] === $_POST['type']) {
$value['product_quantity'] = $_POST['new_qty'];
break; // Stop the loop after we've found the product
}
}
}
?>
<?php
//
if(!empty($_SESSION['basket'])) {
$total = 0;
foreach ($_SESSION['basket'] as $product) {
?>
<form method="post" action="">
<input type="hidden" name="type" value='<?php echo $product["product_id"]; ?>'>
<input type="hidden" name="action" value="update">
<select class="quantity-basket" name="new_qty" onchange="this.form.submit()">
<option value="<?php echo $product['product_quantity']; ?>" hidden><?php echo $product['product_quantity']; ?></option>
<?php
// Drop-down quantity menu
for ($i = 0; $i <=50; $i++) {
?>
<option value="<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>">
<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>
</option>
<?php
}
?>
</select>
</form>
- 1 回答
- 0 关注
- 105 浏览
添加回答
举报