将状态置于项目上

时间:2014-05-03 04:24:10

标签: php html

我想把状态放在项目上。如果数量为0,则应显示Sold Out其他Available。但是我在第16行得到了一个解析错误。为什么会这样?

<?php 

// Connect to the MySQL database  
include "storescripts/connect_to_mysql.php"; 
$dynamicList = "";
$status = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){ 
       $id = $row["id"];
       $product_name = $row["product_name"];
       $price = $row["price"];
       $quantity = $row["quantity"];
       $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
       $status = if($quantity == 0) echo "Sold Out";
                 else echo "Available";
       $dynamicList .= '<table width="100%" border="0" cellspacing="1" cellpadding="6">
         <tr>
          <td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 0px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></a></td>
          <td width="83%" valign="top">       
            <a href="product.php?id=' . $id . '">' . $product_name . '</a><br />
            $' . $price . '<br />
            Quantity =' .$quantity . ' 
            <br /> Status = ' . $status . ' <br />
          </td>
        </tr>
      </table>';
    }
} else {
    $dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>

2 个答案:

答案 0 :(得分:0)

您的代码中存在语法错误:您无法在if语句中为echo语句赋值。请尝试以下内容:

  $status = $quantity == 0 ? "Sold Out" : "Available";
  echo $status;

答案 1 :(得分:0)

这在语法方面完全错误:

$ status = if($ quantity == 0)echo&#34;售罄&#34 ;;              否则回声&#34;可用&#34 ;;

请改为尝试:

if($ quantity == 0)      $ status =&#34;售罄&#34 ;;    其他      $ status =&#34;可用&#34 ;;    echo $ status;