0

As a novice in this and I'm struggling with a small project. I’m putting together a page for INTERNAL orders in a small MTB club. My problem is if there are more than 1 order in the $_SESSION['cart'] and I try to remove the first one [0], then I get “Undefined offset: 0”. I get that is to do with that I remove the first entry and therefor it can’t be found when it lists the array. I just can’t see how I solve it, I have googled and tried different solutions without result. Please advice on how to get forward for me.. Below code is made for this purpose only

enter code here    <?php
session_start(); 
 if(isset($_POST['submit'])){
$total = $_POST['Amount'] * $_POST['Price'];
$cart=array(
'Amount'=>$_POST['Amount'], //Amoun ordered of clothes
'Size'=>$_POST['Size'], //Size of clothes
'Price'=>$_POST['Price'], //Price of clothes
'Product_id'=>$_POST['Product_id'],//Id of clothes for DB
'Product_Name'=>$_POST['Product_Name'], //Name of clothes
'Product_Total'=>$total //Total price    
);      
$_SESSION['cart'][]=$cart;  
}
if(isset($_GET['Reset'])){
unset($_SESSION['cart']);
header('location:demo.php');
}
if(isset($_GET['remove'])){
$do = $_GET['do'];      
unset($_SESSION['cart'][$do]);
 //Redirecting After Unset SESSION
  header('location:demo.php');
  }
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Session Demo/Test</title>
</head>
<body>
<table width="590" border="4" cellspacing="0" cellpadding="5" align="center" >
  <tbody>
    <tr>
      <td width="373">Name of clothes</td>
      <td width="185">
<a href="?Reset">Reset Session</a></td>
    </tr>
  </tbody>
</table>
<form method="POST">
    <table width="590" border="4" cellspacing="0" cellpadding="5" align="center">
  <tbody>
    <tr>
      <td colspan="4" rowspan="5" align="center"><img src="images/Bike_Jersy_short.jpg" width="200" height="200" alt=""/></td>   
      <td height="120" colspan="2"> 
        Clothes description
    </td>
      </tr>
    <tr>
      <td width="112">Price:</td>
      <td>300,00 Kr.</td>
    </tr>
    <tr>  
      <td width="112">Amount:</td>
      <td width="116">
            <input name="Amount" type="number" id="Amount" tabindex="1" value="" size="1" required >
        </td>
    </tr>
    <tr>  
      <td>Size:</td>
      <td>
              <select name="Size"  id="Size" tabindex="2" required>
       <option value=""></option>
          <option value="XS">XS</option>
          <option value="S">S</option>
          <option value="L">L</option>
          <option value="XL">XL</option>
          <option value="2XL">2XL</option>
          <option value="3XL">3XL</option>
          <option value="4XL">4XL</option>
          <option value="5XL">5XL</option>
          <option value="6XL">6XL</option>
        </select>
        </td>
    </tr>
         <tr>  
      <td>Add:</td>
      <td>
         <input type="submit" name="submit" id="submit" value="Add">
           <input name="Price" type="hidden" id="Price" value="300">
        <input name="Product_id" type="hidden" id="Product_id" value="1">
          <input name="Product_Name" type="hidden" id="Product_Name" value="Name of clothes">        
           </td>
    </tr>
  </tbody>
</table>
       </form>      
<p></p>
    <?php
    if(empty($_SESSION['cart'])){ 
        ?>
    <table width="898" border="4" cellspacing="0" cellpadding="5" align="center">
  <tbody>
    <tr>
        <td>You have no items in yet </td>
    </tr>
  </tbody>
</table>
    <?php
    }
    else{
        ?>
    <table width="900" border="4" cellspacing="0" cellpadding="5" align="center">
  <tbody>
    <tr>
      <td width="109">Name</td>
      <td width="31">Amount</td>
      <td width="33">Size.</td>
      <td width="63">Price</td>
      <td width="55">Total</td>
      <td width="45">Remove</td>
    </tr>     
      <?php for($i = 0 ; $i < count($_SESSION['cart']) ; $i++) {
      ?>      
    <tr>
      <td><?php echo $_SESSION['cart'][$i]['Product_Name'];?></td>
      <td><?php echo $_SESSION['cart'][$i]['Amount'];?>
      </td>
      <td><?php echo $_SESSION['cart'][$i]['Size'];?></td>
      <td><?php echo $_SESSION['cart'][$i]['Price'];?></td>
      <td><?php echo $_SESSION['cart'][$i]['Product_Total'];?></td>
      <td><a href="demo.php?remove=remove&do=<?php echo $i;?>" title="Remove">Remove</a>            
          <?php
          }
            ?>
      </td>
    </tr>     
    <?php }  ?>   
  </tbody>
</table>                
</body>
</html>
1

1 Answer 1

1

Use the array_shift() function.

Like so:

$value = array_shift($_SESSION['cart']);

This will remove the first element in the array regardless of the current index. $value is the value of the element that was removed.

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

1 Comment

This will solver your problem. It will also modify all numeric keys to start from zero again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.