php 用 0 值隐藏 text/links

Hiding text/links with 0 value results by php

我的 html table 中有一个链接列表,例如:胸罩:5 件,胸罩:15 件,毯子:0 件,毛巾:0 件。我需要 "Blanket" 和 "Towel" 以及其他列表位置”,其中 0 值从列表中隐藏直到它们为 0 值,在值更改为超过 0 个(例如:1 个或更多)后它们将显示。"Blanket: 0pcs" and "Towel: 0pcs" and "other goods" with 0pcs value not hiding but still show 0pcs value.我做错了什么或者我的错误是什么?也许还有另一种方法可以做到这一点。我可以'不懂,谢谢帮助。

<?
  $resultonshafa = mysql_query("SELECT count(customers_id) from tbl_customers WHERE shafa='Y'");
  $resultonshafasaled = mysql_query("SELECT count(customers_id) from tbl_customers WHERE shafa='Y' and saled='Y'");
  $resultonolx = mysql_query("SELECT count(customers_id) from tbl_customers WHERE last_name='Y'");
  $resultonolxsaled = mysql_query("SELECT count(customers_id) from tbl_customers WHERE last_name='Y' and saled='Y'");

  $topmenuOnShafa = mysql_result($resultonshafa, 0);
  $topmenuNotOnShafa = mysql_result($resultonshafasaled, 0);
  $topmenuOnOlx = mysql_result($resultonolx, 0);
  $topmenuNotOnOlx = mysql_result($resultonolxsaled, 0);

  $topmenuOnOlxText = "Blanket : ";
  $topmenuOnShafaText = "Towel : ";
 ?>

<?php if ($topmenuNotOnOlx!=0): ?>
<span class="saled-warning"><a href="some_link" target="_self"><?=$topmenuNotOnOlx;?></a></span>
<?php endif; ?>
<?php if ($topmenuOnOlx!=0): ?><?=$topmenuOnOlxText;?><?php endif; ?>
<?php if ($topmenuOnOlx!=0): ?>
<a href="some_link" target="_self"><?=$topmenuOnOlx;?></a>
<?php endif; ?>

<?php if ($topmenuNotOnShafa!=0): ?>
<span class="saled-warning"><a href="some_link" target="_self">
<?=$topmenuNotOnShafa;?></a></span>
<?php endif; ?>

您只需 运行 一次查询即可获取所有信息:

SELECT
   SUM(shafa='Y') AS only_shafa,
   SUM(shafa='Y' AND saled='Y') as saled_and_shafa,
   SUM(last_name='Y') AS only_lastname,
   SUM(last_name='y' AND saled='Y') AS saled_and_lastname
FROM ...

MySQL 会自动将那些 =/and 测试返回的布尔值 true/false 转换为整数,并将它们相加。然后你只运行这个ONE查询,获取一行结果:

$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);

然后用 $row['only_salend'] 等测试这些值。

但请注意,您的代码只是假设查询永远不会失败。这是一个糟糕的假设。如果查询失败,您将返回布尔值 FALSE。然后你会尝试从那个错误中得到一个结果,导致另一个错误。由于您的结果现在是布尔值 FALSE,false != 0 将评估(错误地)评估为布尔值 TRUE.