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