-1

Need help for code that will appearing not available button using if else that can disable to adding in a cart if the food is not available

<?php
include('connection.php');

$cat = $_GET['category'];
$result = mysql_query("SELECT * FROM menu where category='$cat' ");
while ($row = mysql_fetch_array($result)) {
    echo '
<form name="Form1" method="post" action="">
<img src="' . $row['picture'] . '" alt="" align="top" border="0" style="width:82px;height:75px;">
<font style="font-size:19px" color="#000000" face="Arial">' . $row['foods'] . '</font></div>
<font style="font-size:13px" color="#000000" face="Arial">' . $row['price'] . ' pesos</font>
<a href="addcart2.php?productid=' . $row['productid'] . '&&category=' . $row['category'] . '"><img src="images/button.png" id="Image2" alt="" align="top" border="0" style="width:80px;height:26px;"></a>
</form> ';
}
?>
Nik
  • 2,885
  • 2
  • 25
  • 25

1 Answers1

0

First of all, you have several issues in your code:

  1. Your sql query is not safe. Read this
  2. Do not use mysql_* functions. Read this
  3. If you generate html code in a loop, then name and id attributes must be unique. Replace name="Form1" with e.g. name="Form' . $row['productid'] . '"
  4. You have double & sign in the url query: &&category=. Fix it.

Answering the root question: how to use if statement in a while loop.

<?php
while ($row = mysql_fetch_array($result)) {
    if (!empty($row['foods'])) { // assuming you have a non-zero value when the product is available
        // echo html with a product card
    } else {
        // echo html with a not available button
    }
}
?>

See the documentation how control structures work in PHP.

Nik
  • 2,885
  • 2
  • 25
  • 25