简体   繁体   中英

Remove an item from array

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?

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.

尝试像这样删除。

array_splice($_SESSION['basket'], $i, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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