我有一个菜单块,其中包含一些链接,如: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; ?>
您可以检查项的值是否为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条件。
use
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