How do I fix this issue, you see in the picture below, my code creates a table successfully and echos the first item correctly, but if any other item is added to the cart, it gets displayed below the table, outside the border, not in a row.
Desired Result: For ‘Sara Lee Muffin’ and the quantity, and price (example) to be inside the table in it’s own row and remove button. (Basically set out the exact same way a the Toasted Sandwhich, but below it)
My code is below the image.
JavaScript
x
<body>
<br>
<h3>Current shopping cart:</h3>
<table style="border: 1px solid black;">
<?php
//If the cart is not empty, then create a table for the Items
if(!empty($_SESSION['shopping_cart'])) {
?>
<tr><th>Item name</th><th>Quantity</th><th>Price</th><th></th></tr>
<?php
if(!empty($_SESSION['shopping_cart'])) {
foreach($_SESSION['shopping_cart'] as $keys => $values) {
?>
<tr>
<td style="text-align:center"><?php echo $values['item_name'];?></td>
<td style="text-align:center"><?php echo $values['item_quantity'];?></td>
<td style="text-align:center">$<?php echo $values['item_price']*$values['item_quantity'];?>
</td>
<td><form action="" method="get">
<input type="submit" class="btn btn-danger" name="remove" value="Remove">
<input type="hidden" name="hidden_id" value="<?php echo $values['item_id']; ?>">
</form></td>
</tr>
</table>
<br>
<?php
}
}
?>
<form action="" method="get">
<input type="submit" class="btn btn-primary" name="payment" value="Confirm">
</form>
<?php
}
else {
echo "No Items added yet.";
?><br>
<?php
}
?>
</body>
Advertisement
Answer
You have a closing </table>
inside your foreach loop, so every item in the array prints out a </table>
tag.
You should add another check for the shopping cart after the loop, and if it exists, print the closing </table>
tag.