I create simple add to cart system using php mysqli when user click that button ( add to cart ) they will be redirected to view cart page. Where is the problem? My system working nice, but on final page ( view single article ) i have displayed several buttons ( add to cart ). If i have in database 2 products, on final page ( product page ) i wil get 2 buttons ( add to cart ) and that is what i don't need. I need to display only button of that product. Example:
I have 2 products in 2 diferent categories and their link is:
- https://website.com/en/product-category/product.php?id=23
- https://website.com/en/product-category/product.php?id=35
When user open product with id 23 on that page will display button from article 23 and article 35. And i don't need that. I need on product with id 23 display only button for that article.
On top page code:
//initialize cart if not set or is unset
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
//unset qunatity
unset($_SESSION['qty_array']);
This is my code:
<?php
$conn = new mysqli('localhost', 'root', 'password', 'database');
$sql = "SELECT * FROM post";
$query = $conn->query($sql);
$inc = 4;
while($row = $query->fetch_assoc()){
$inc = ($inc == 4) ? 1 : $inc + 1;
if($inc == 1) echo "<div class='row text-center'>";
?>
<a href="../../../en/add_cart.php?post_id=<?php echo $row['post_id']; ?>" target="">Add to cart</a>
<?php
}
if($inc == 1) echo "<div></div><div></div><div></div></div>";
if($inc == 2) echo "<div></div><div></div></div>";
if($inc == 3) echo "<div></div></div>";
//end product row
?>
Where is problem and how to fix to display only single button ( add to cart ) not all 2 or ? buttons?