我正在使用购物车项目,用户可以在购物车中添加多个商品,并从购物车更新或删除商品。除了每个新添加的一个项目之外,我还分配selection_id。我逐个增加ID,每次添加新项目到购物车代码工作正常。
我在这里向您展示我的购物车代码。当我点击添加项目到购物车时,下面的代码已执行。
if(!isset($_SESSION['cart'])){
$_SESSION['cart']=array();
}
$no_of_itm_in_cart= count($_SESSION[cart]);
//$sl_id is Selction Id
$sl_id=$no_of_itm_in_cart+1;
$item_sl = array();
$item_sl['item_sl_id']= $sl_id;
$item_sl['item_id']=$pid;
$item_sl['item_qty'] =$quantity;
$_SESSION['cart'][$sl_id] = $item_sl;
请与我分享,如何显示每个元素以及如何更新和删除“item_id”为参数的任何元素。
答案 0 :(得分:1)
echo '<table>
<tr>
<th>SL ID</th>
<th>ID</th>
<th>Quantity</th>
<th>Actions</th>
</tr>';
foreach($_SESSION['cart'] as $row){
echo ' <tr>
<td>'.$row['item_sl_id'].'</td>
<td>'.$row['item_id'].'</td>
<td>'.$row['item_qty'].'/td>
<td>
<button class="remove" data-id="'.$row['item_sl_id'].'">Remove</button>
<button class="edit" data-id="'.$row['item_sl_id'].'">Edit</button>
</td>
</tr>';
}
echo '</table>';
JS。
$('.delete').click(function(e){
id = $(this).data('id');
$.ajax({
url : 'path/to/delete.php',
type : 'POST',
data : {'id':id},
timeout : 30000,
success: function(e) {}
});
});
delete.php
unset($_SESSION['cart'][$_POST['id']]);die;