将我的 "Add to Cart" link 替换为 "Out of Stock"

Replacing my "Add to Cart" link with "Out of Stock"

为了 class 项目,我正在尝试创建一个模型商店。我的工具是 XAMPP 3.2.2 和 phpMyAdmin。稍后会出现一个实际可用的 "Add to cart" 按钮(现在我只是使用 link 作为占位符),但目前,我正在尝试找出如何替换 button/link,当库存为 0 时显示一条消息 "Out of Stock"。

这是我用来显示产品页面的代码;它几乎肯定是混乱的,并且可能有十几种更好的方法来做到这一点,但现在,它完成了工作:

<?php
$sql = "SELECT * FROM webstore.Products order by category";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
 // output data of each row
 while($row = $result->fetch_assoc()) {

      echo '<table border="2" width="100%">';
      echo '<tr>';
                   echo '  <td width="20%">' . $row["Name"] . '</td>';
                  echo '  <td width="20%">' . $row["Descr"] . '</td>';
                   echo '  <td width="20%">' . $row["Price"] . '</td>';
                   echo '  <td width="20%">' . '<img src =' . $row["IconURL"] . '>' . '</td>';
                   echo '  <td width="20%">' . '<a href ="http://localhost:81/shopping_cart.php">Add to cart</a>' . '</td>';
                   echo '  </tr> ';  
                    echo '</table>';
 }
} else {
 echo "0 results";
}

输出如下所示: Results

我现在不是在寻找 "better practices;",我只是需要一种方法,当产品库存为 0 时,用 "Out of Stock" 替换购物车 link。

向 while 循环添加一个检查,例如:

while($row = $result->fetch_assoc()) {

  echo '<table border="2" width="100%">';
  echo '<tr>';
  echo '  <td width="20%">' . $row["Name"] . '</td>';
  echo '  <td width="20%">' . $row["Descr"] . '</td>';
  echo '  <td width="20%">' . $row["Price"] . '</td>';
  echo '  <td width="20%">' . '<img src =' . $row["IconURL"] . '>' . '</td>';
if($row['stock'] >0){  
echo '  <td width="20%">' . '<a href ="http://localhost:81/shopping_cart.php">Add to cart</a>' . '</td>';
}else{
echo '<td width="20%">Out of Stock</td>';
}  

echo '  </tr> ';  

  echo '</table>';
 }