php 隐藏具有 0 个查询值结果的链接
Hiding links with 0 query value results by php
我有一个包含一些 link 的菜单块,例如:Some_link1:5 个,Some_link2:13 个,Some_link3:0 个,Some_link4 : 0 个
我想用 0 个值隐藏 link "Some_link"。我用 MySQL 查询编写了代码,但它不起作用! "Some_link" 0 个未隐藏但仍显示 0 个值。
我做错了什么或我的错误是什么?我不明白。谢谢你的帮助。
<?
$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; ?>
使用
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";
?>
可以检查item的值是否为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 条件。
我有一个包含一些 link 的菜单块,例如:Some_link1:5 个,Some_link2:13 个,Some_link3:0 个,Some_link4 : 0 个 我想用 0 个值隐藏 link "Some_link"。我用 MySQL 查询编写了代码,但它不起作用! "Some_link" 0 个未隐藏但仍显示 0 个值。 我做错了什么或我的错误是什么?我不明白。谢谢你的帮助。
<?
$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; ?>
使用
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";
?>
可以检查item的值是否为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 条件。