0

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?

4
  • note that second item is item #1. Maybe you're removing 2nd item (#2) which is actually 3rd? Commented Oct 2, 2013 at 14:13
  • Consider paring down your code to the essential question. That is a lot of code to ask a simple question. Commented Oct 2, 2013 at 14:21
  • What code are you using to remove it? Commented Oct 2, 2013 at 14:21
  • if (isset($_POST['removeItem'])) { unset($_SESSION['basket'][$i]); } Commented Oct 2, 2013 at 14:21

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Yooouuri If this solved your issue please accept it as the answer so that it is known your issue has been fixed :)
0

Try to remove like this.

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

1 Comment

If the POST removeItem is in the for loop. It always take the first button. So i need to fix that!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.