dst3605528 2013-10-02 14:11 采纳率: 0%
浏览 14

从数组中删除项目

I am creating a webshop with PHP and use sessions to save temp items.

<form method="post" action="basket.php">
  <table border="1" style="width:400px;">
    <tr>
      <td style="width:35%;">Product</td>
      <td style="width:30%;">Aantal</td>
      <td style="width:25%;">Totale prijs</td>
      <td style="width:10%;"></td>
    </tr>
    <?php
    for($i = 0; $i < count($_SESSION['basket']); $i++) {    
      $id     = $_SESSION['basket'][$i]['itemId'];
      $number = $_SESSION['basket'][$i]['number'];

      $result = $mysqli->query('SELECT * FROM items WHERE id = "' . $id . '"');
      $row    = $result->fetch_assoc();

      $total = $number * $row['price'];
      ?>
    <tr>
      <td>
        <a href="view_item.php?id=<?php echo $id; ?>">
          <?php echo $row['name']; ?>    
        </a>
      </td>
      <td><?php echo $number; ?></td>
      <td>&euro; <?php echo $total; ?></td>
      <td><input type="submit" name="removeItem[]" value="Verwijder" /></td>
    </tr>
    <?php
    if (isset($_POST['removeItem'])) {
      unset($_SESSION['basket'][$i]);
    }

    $_SESSION['basket'] = array_values($_SESSION['basket']);
  }
  ?>
  </table>
</form>

When i set 3 items into the session.

Array
(
  [basket] => Array
    (
      [0] => Array
        (
          [itemId] => 1
          [number] => 1
          [timestamp] => 1380722942
        )
      [1] => Array
        (
          [itemId] => 1
          [number] => 1
          [timestamp] => 1380722944
        )
      [2] => Array
        (
          [itemId] => 1
          [number] => 1
          [timestamp] => 1380722945
        )
    )
)

And remove the second item, it removes the last item also..

What goes wrong?

Can somebody help me?

  • 写回答

2条回答 默认 最新

  • douliao7930 2013-10-02 14:25
    关注

    You are unsetting the SESSION variable for basket inside the for loop. So if the user tries to remove the 2nd item, the for loop will continue and all items after that will also be unset.

    You should exit the for loop after unsetting the correct item

    if (isset($_POST['removeItem'])) {
        unset($_SESSION['basket'][$i]);
        break;
    }
    

    You would then also want to move the last line,

    $_SESSION['basket'] = array_values($_SESSION['basket']);,

    outside of the for loop so that it gets executed.

    EDIT

    After closer inspection I'm not sure why you are unsetting the SESSION variable inside the for loop, but I think you need to refactor your code. Breaking out of the for loop would solve your issue with multiple items being removed but would also cause your table to be incomplete (no items in the basket after the one you remove would get displayed). You should handle the removal of an item in a separate place than the creation of the table. I would move unsetting the SESSION variable for the item you want to remove to the action you call when this form is submitted.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c