1

I am new to PHP and I am tried to show the stored cart item SESSION variable into the HTML table. but while doing for each loop, the first row in the table is showing blank. even after adding items into the cart also it showing the first-row blank. Please help with this.

Here is my method:

  <table class="table">
 <thead>
    <tr>
       <th>sl</th>
       <th>Name</th>
       <th>Price</th>
       <th>Qty</th>
       <th>Total Price</th>
       <th>Update</th>
       <th>Delete</th>
    </tr>
 </thead>
 <tbody>
    <?php
       $bill= 0;
         $sno = 1;
       
       foreach($_SESSION as $products){
       $p=0;
       $q=0;
       echo "<tr>";
       echo "<td>".$sno++."</td>";
       echo "<form action='edit_cart.php' method='post'>"; 
       if (!$products) $products = array();    
         foreach($products as $key =>$value){
             //print_r($products);
       if($key == 2){
       echo "<td><input type='number' name='name$key' class='form-control col-xl-3 text-center' min='1' value='".$value."'></td>";     
       $q = $value;    
       }else if($key == 1){
       echo "<input type='hidden' name='name$key' class='form-control' value='".$value."'>";        
        echo "<td>".$value."</td>";     
       $p = $value;       
       }else if($key == 0){
       echo "<input type='hidden' name='name$key' class='form-control' value='".$value."'>";       
       echo "<td>".$value."</td>";   
       } 
       } 
       $bill= ($q * $p);
       echo "<td>".($bill)."</td>";
       echo "<td><input type='submit' name='event' value='Update' class='btn btn-sm btn-warning'></td>";
       echo "<td><input type='submit' name='event' value='Delete' class='btn btn-sm btn-danger'></td>";  
              echo "</form>"; 
       echo "</tr>";
       
       }   
         ?>
 </tbody>
</table>

Screenshot: https://i.stack.imgur.com/VdvMV.png

  • 3
    For starters, try to fix your HTML structure. A form cannot be inserted anywhere in a table, it can only be contained either in a single `td` or it should wrap the whole table. See [Form inside a table](https://stackoverflow.com/questions/5967564/form-inside-a-table) – El_Vanja May 14 '21 at 08:54
  • You don't have an ending bracket `}` on the foreach loop.....Also, having proper format and/or neat tabbing would help distinguish the problem a lot easier. – Crimin4L May 15 '21 at 00:21

0 Answers0