通过php隐藏具有0值结果的文本/链接

时间:2016-09-01 14:07:23

标签: php mysql show-hide

我的html表中有一个链接列表,如:Bra:5psc,Brasets:15pcs,Blanket:0pcs,Towel:0pcs。我需要那个“Blanket”和“Towel”以及其他列表位置“0值从列表中隐藏,直到它们的值为0,值变为超过0pcs(例如:1pcs或更多)后,他们会显示。”毯子:0pcs“和“毛巾:0pcs”和“其他商品”0pcs值没有隐藏,但仍然显示0pcs值。我做错了什么或我的错误?也许有另一种方法来做到这一点。我无法理解。谢谢你寻求帮助。

<?
  $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; ?>

1 个答案:

答案 0 :(得分:1)

您可以运行单个查询来获取所有这些信息:

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会将那些= /和test返回的布尔值true / false自动转换为整数,并将它们相加。然后你只运行这个ONE查询,获取一行结果:

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

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

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