0

I'm doing a very simple project for my assignment and it's about shopping cart. I've created a cart page by using SESSION with array and index to keep track user's order. The problem is whenever I'm deleting a meal from my cart list, there'll be an error which is "Notice: Undefined offset: 1", "Notice: Undefined offset: 2", etc. I'm attaching my codes here:

This is my cart page. The error said it has something to do on the line that I have commented:

<form method="POST" action="save_cart.php">
<table class="table">
<tbody>

<thead>
      <th></th>
      <th>Name</th>
      <th>Price</th>
      <th>Quantity</th>
      <th>Subtotal</th>
</thead>


  <?php
        //initialize total
        $total = 0;
        if(!empty($_SESSION['cart'])){

        //create array of initail qty which is 1
        $index = 0;
        if(!isset($_SESSION['qty_array'])){
          $_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
        }
        $sql = "SELECT * FROM meal WHERE meal_id IN (".implode(',',$_SESSION['cart']).")";
        $query = $conn->query($sql);
          while($row = $query->fetch_assoc()){
            ?>

          <tr>

              <td class="removebtn" style="text-align: center;">
                <a href="delete_item.php?id=<?php echo $row['meal_id']; ?>&index=<?php echo $index; ?>" >Remove</a>
              </td>
              <td><?php echo $row['meal_name']; ?></td>
              <td>RM <?php echo number_format($row['meal_price'], 2); ?></td>
              <input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
              <td class="qtyinput" ><input type="number" min="1" max="5" class="form-control" value="<?php echo $_SESSION['qty_array'][$index]; ?>" name="qty_<?php echo $index; ?>"></td>

              //THE ERROR IS HERE

              <td>RM <?php echo number_format($_SESSION['qty_array'][$index]*$row['meal_price'], 2); ?></td>
              <?php $total += $_SESSION['qty_array'][$index]*$row['meal_price']; ?>
            </tr>
            <?php
            $index ++;
          }
        }
        else{
          ?>
          <tr>
            <td colspan="5" class="text-center"  style="text-align: center;">No Meal in Cart</td>
          </tr>
          <?php
        }

      ?>
      <tr>
        <td colspan="4" align="left"><b>Total</b></td>
        <td><b>RM <?php echo number_format($total, 2); ?></b></td>
      </tr>
</tr>

Here's my delete page:

session_start();

//remove the id from our cart array
$key = array_search($_GET['meal_id'], $_SESSION['cart']);   
unset($_SESSION['cart'][$key]);

unset($_SESSION['qty_array'][$_GET['index']]);
//rearrange array after unset
$_SESSION['qty_array'] = array_values($_SESSION['qty_array']);

$_SESSION['message'] = "<script>alert('Meal is deleted from your cart');</script>";
header('location: cart.php');

I really need your help. Thanks in advance!

znf96
  • 61
  • 1
  • 8

1 Answers1

1

Its an index error because index doesn't exists in the array replace your html php code inside while loop with this one.

<tr>

<td class="removebtn" style="text-align: center;">
<a href="delete_item.php?id=<?php echo $row['meal_id']; ?>&index=<?php echo $index; ?>" >Remove</a>
</td>
<td><?php echo $row['meal_name']; ?></td>
<td>RM <?php echo number_format($row['meal_price'], 2); ?></td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td class="qtyinput" ><input type="number" min="1" max="5" class="form-control" value="<?php echo (isset($_SESSION['qty_array'][$index]) ? $_SESSION['qty_array'][$index] : 0); ?>" name="qty_<?php echo $index; ?>"></td>

//THE ERROR IS HERE

<td>RM <?php 
$qty = (isset($_SESSION['qty_array'][$index]) ? $_SESSION['qty_array'][$index] : 0);
echo number_format($qty *$row['meal_price'], 2); ?></td>
<?php $total += $qty*$row['meal_price']; ?>
</tr>
Ashfaque Ali Solangi
  • 1,883
  • 3
  • 22
  • 34
  • Thank you sir, but the Undefined Offset is still there when I remove the items. It's on the line . And the item doesn't removed from the list. I've attached the pic above. How do I fix it, sir? – znf96 Dec 05 '18 at 12:21
  • i think the issue is with query maybe that is not checking the actual item is in cart or not ? can you print this query $sql = "SELECT * FROM meal WHERE meal_id IN (".implode(',',$_SESSION['cart']).")"; echo $sql;exit(); and check what results come out. – Ashfaque Ali Solangi Dec 05 '18 at 12:51
  • can you replace this line $total += $_SESSION['qty_array'][$index]*$row['meal_price']; with $total += $qty*$row['meal_price']; – Ashfaque Ali Solangi Dec 05 '18 at 13:01
  • Hi sir, can I have your email? I want to ask some questions about the problems I'm having with my project. I hope you don't mind. – znf96 Dec 07 '18 at 18:29
  • @znf96 you can contact at info@ashfaqsolangi.com – Ashfaque Ali Solangi Dec 07 '18 at 20:19