php - php隐藏查询值结果为0的链接

标签 php mysql show-hide

我有一个菜单 block ,其中包含一些链接,例如:Some_link1:5pcs,Some_link2:13pcs,Some_link3:0pcs,Some_link4:0pcs。 我想隐藏值为 0pcs 的链接“Some_link”。我用 MySQL 查询编写了代码,但它不起作用! “Some_link” 0pcs 没有隐藏,但仍然显示 0pcs 值。 我做错了什么或者我的错误是什么?我无法理解。感谢您的帮助。

<?
  $resultonline = mysql_query("SELECT count(customers_id) from tbl_customers WHERE active='Y' and saled='N'");
  $resultonshafasaled = mysql_query("SELECT count(customers_id) from tbl_customers WHERE shafa='Y' and saled='Y'");
  $resultonlinenonactive = mysql_query("SELECT count(customers_id) from tbl_customers WHERE active='N' and saled='N'");

  $topmenuNotOnShafa = mysql_result($resultonshafasaled, 0);
  $topmenuonline = mysql_result($resultonline, 0);
  $topmenuoffline = mysql_result($resultonlinenonactive, 0);

  $topmenuonlineText = "Some text : ";
  $topmenuOnShafaText = "Some text 2 : ";
 ?>

<?php if ($topmenuonline!=0): ?><?=$topmenuonlineText;?><?php endif; ?>
<?php if ($topmenuonline!=0): ?><a href="some_link" target="_self"><?=$topmenuonline;?></a>
<?php endif; ?>
<?php if ($topmenuoffline!=0): ?> / <a href="some_link" target="_self"><?=$topmenuoffline;?></a>
<br /><?php endif; ?>

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

最佳答案

你可以检查该项的值是否为0,如果不为0则打印它:

示例:

 <?php

 $items='0';

 if(isset($items)){
     if($items != 0){
         echo "<a href='non_zero_item.php'>Item from menu (".$items.")";
     } else {
         echo "Oh sorry, there are no items!";
     }
 } else {
     echo "items variable is not declared!";
 }

 ?>

在此示例中,您将获得 else 条件,如果将变量 $items 更改为 1,您将打印 html 代码。这是一个小测试,变量可以是mysql查询结果、手动输入等。

如果您不想在值为 0 或未声明时打印任何内容,就像我了解您希望的那样,您只能这样做:

 <?php

 $items='1';

 if(isset($items)){
     if($items != 0){
         echo "<a href='non_zero_item.php'>Item from menu (".$items.")";
     }
 }

 ?>

对于调试,我建议您始终使用 else 条件。

关于php - php隐藏查询值结果为0的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39271299/

相关文章:

php - 未知列,错误

mysql - 导入两个数据集后连接表的最有效方法

mysql - 使用 MySQL 中同一选择查询的数据运行多次插入

javascript - 'Show More' & 'Show Less' with jQuery for an article website

php - 如何在 PHP 中将文本框的值复制到我的查询中

javascript - 手动重定向移动桌面版本

php对mysql选择行进行自然顺序排序

android - 在Android中使用两个表时出现SQL错误

javascript - 如果某个输入字段已填写(但未提交),如何仅显示 div?

javascript - 在另一个选择列表中进行选择后如何显示/隐藏选择列表?