简体   繁体   English

从会话购物车中删除项目数组

[英]Remove array of items from session shopping cart

I have an array of items kept in session cart.我在会话购物车中保存了一系列项目。 It gets populated in a table.它被填充到一个表中。 each row has an index and a button to remove this particular item(items array) from $_SESSION['cart']..每行都有一个索引和一个按钮,用于从 $_SESSION['cart'].. 中删除此特定项目(项目数组)。

This is the code I have at the moment:这是我目前的代码:

 $i = 0;
 foreach($_SESSION['cart'] as $item)
{
    //Populate items in a table  ?>
<tr>
<td><center><?php echo $i; ?></center></td>
<td><center><?php echo $item['item'];?></center></td>
<td><center><?php echo '£'. $item['unitprice'];?></center></td>
<td><center><?php echo $item['quantity'];?></center></td>
    <td><center><?php echo '£'.($item['unitprice'] * $item['quantity']) ?></center></td>
<td><input type="submit" value="Remove" Onclick =  unset($_SESSION['cart'][$i]); ></td>
</tr>
<?php
 $total += ($item['unitprice'] * $item['quantity']);
$i++;
}

All I want to do is to remove one single row of data (each row contains index, item, item price, total(if there are more than one item) and remove button) from session cart.我想要做的就是从会话购物车中删除一行数据(每行包含索引、项目、项目价格、总计(如果有多个项目)和删除按钮)。 Thanks in advance..提前致谢..

The unset is correct, but PHP is executed on the servers side. unset 是正确的,但是 PHP 是在服务器端执行的。 So because this is a client action, you'll need to use JavaScript to pick up the action.因此,因为这是一个客户端操作,所以您需要使用 JavaScript 来选择该操作。 (you could also use an old fasion form as well). (您也可以使用旧的 fasion 形式)。 I prefer to use jQuery since it allready has the built in ajax support and makes it easy.我更喜欢使用 jQuery,因为它已经内置了 ajax 支持并且很容易使用。 Basically, just call a function from the onClick that passes the "ID" of the element you want to delete.基本上,只需从传递要删除的元素的“ID”的 onClick 调用一个函数。 Then that function will make an AJAX call to another script that will take that ID and remove the element.然后该函数将对另一个脚本进行 AJAX 调用,该脚本将获取该 ID 并删除该元素。 When that script returns a success, have JavaScript delete (or hide) the element.当该脚本返回成功时,让 JavaScript 删除(或隐藏)该元素。 This way the user never leaves or refreshes the page, and the script can do some extra cleanup (like database updates, session updates, validate request, etc.)这样用户永远不会离开或刷新页面,脚本可以做一些额外的清理(如数据库更新、会话更新、验证请求等)

i = 0;
 foreach($_SESSION['cart'] as $item)
{
    //Populate items in a table  ?>
<tr>
<td><center><?php echo $i; ?></center></td>
<td><center><?php echo $item['item'];?></center></td>
<td><center><?php echo '£'. $item['unitprice'];?></center></td>
<td><center><?php echo $item['quantity'];?></center></td>
    <td><center><?php echo '£'.($item['unitprice'] * $item['quantity']) ?></center></td>
<td><input type="submit" value="Remove" onClick="RemoveItem(<?= $i ?>)" ></td>
</tr>
<?php
 $total += ($item['unitprice'] * $item['quantity']);
$i++;
}

Here I've changed the onClick section to use a javascript call instead.在这里,我已将 onClick 部分更改为使用 javascript 调用。 You'll notice I used the <?= tag instead of a full <?php echo .您会注意到我使用了<?= tag而不是完整的<?php echo This is because as of php5 you can use the shorthand <?= to echo an expression.这是因为从 php5 开始,您可以使用简写<?=来回显表达式。

In your script section you can do something like this.在你的脚本部分,你可以做这样的事情。 I use POST since it's more secure.我使用 POST 因为它更安全。

function RemoveItem(id) {
   $.post('script-to-remove.php',
      { ItemID: id },
      function(data) {
         if(data==='success') {
            //Remove item from DOM
         } else alert("There was an error!"); //If you want error handling
      }
   );
}

For removing items from the dom, its a bit trickier with tables since none of the cool effects will really work, such as fadeOut, slideUp, animate, etc. I would look into reformatting it with divs.为了从 dom 中删除项目,它对表格有点棘手,因为没有一个很酷的效果真的有效,例如淡出、滑动、动画等。我会考虑用 div 重新格式化它。

As for the php script it calls, its as simple as calling the unset.至于它调用的php脚本,就跟调用unset一样简单。 You can develop it more, so as to possibly check the validity of the request, but for now, I'm just going to do the unset.您可以进一步开发它,以便可能检查请求的有效性,但现在,我只是要取消设置。

<?php 
$ItemID = isset($_POST['ItemID'])?intval($_POST['ItemID']):-1;
if($ItemID<0) die("Invalid ID"); //Change this if you want
if(isset($_SESSION['cart'][$ItemID])) unset($_SESSION['cart'][$ItemID]);
echo 'success';
?>

Basically in this, I first check that the right parameter came in, make sure it's an integer and save it, otherwise I default it to -1.基本上在这里,我首先检查输入的参数是否正确,确保它是一个整数并保存它,否则我默认它为-1。 Then if the Item ID is under 0 (so definatly wrong) I tell the script to output the message and quit.然后,如果项目 ID 低于 0(所以肯定是错误的),我会告诉脚本输出消息并退出。 Finally I check that there is a value for that ID or index as it really is, and unset it.最后,我检查该 ID 或索引是否有实际值,然后取消设置。 If it makes it to the end, output the success.如果成功,则输出成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM