通过php隐藏具有0个查询值结果的链接

时间:2016-09-01 12:14:18

标签: php mysql show-hide

我有一个菜单块,其中包含一些链接,如:Some_link1:5pcs,Some_link2:13pcs,Some_link3:0pcs,Some_link4:0pcs。 我想隐藏链接" Some_link" 0pcs值。我用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; ?>

2 个答案:

答案 0 :(得分:1)

使用

mysql_num_rows

<?php

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

?>

http://php.net/manual/en/function.mysql-num-rows.php

答案 1 :(得分:1)

您可以检查项目的值是否为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.")";
     }
 }

 ?>

对于debuging,我建议你总是使用其他条件。