php if 语句用于显示某些 html 块
我目前正在尝试检查我的数据库表以查看用户的级别是否等于或小于 10,如果是,则显示整个 html 列表,如果不是且等于或小于 5,则仅显示列表的剩余部分。这就是我开始的地方:
<?php
$levels = mysql_query("SELECT level FROM users");
if ($levels <= 10) {
?>
html list
<?php
if ($levels <= 5)
?>
html list
<?php
}
?>
我试图理解它,但我似乎无法理解它。
I am currently trying to check my db table to see if a user's level is equal to or less than 10, if so then show an entire html list, if not and is equal or less than 5 show only the remaining part of the list. Here's where I started:
<?php
$levels = mysql_query("SELECT level FROM users");
if ($levels <= 10) {
?>
html list
<?php
if ($levels <= 5)
?>
html list
<?php
}
?>
I am trying to wrap my head around it, I just can't seem to get it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
正如David Fells 所说,确保您首先得到结果:)
编辑:我认为我误读了您的问题。试试这个:
我对此感到困惑:
..as 0-5 将小于或等于 10。请原谅混淆,GL!
Try this:
As David Fells said, make sure you are getting a result first :)
EDIT: I misread your question I think. Try this instead:
I was confused by this:
..as 0-5 will be less or equal to ten. Please excuse the mixup, GL!
这样就完全清楚了吗?您需要提供当前用户的 ID,然后需要实际从 $result 中检索值。 $result 是资源类型的变量,您必须使用专门设计的函数来处理它。
Does that clear it up at all? You need to supply the ID of the current user, and then you need to actually retrieve the value from the $result. The $result is a variable of type resource, and you have to work on it with functions designed to do so.
您需要在级别 <= 10 之前询问级别 <= 5,因为级别 <= 10 总是会找到级别 <= 5。
You need to ask for the levels <= 5 before levels <= 10, because levels <= 10 will always find levels <= 5.
该代码有很多错误
- 首先,
返回一个包含所有用户级别的数组。您想使用 WHERE 子句查询特定用户的级别。
接下来,当您想要区分 <=5 和 5 &10 之间时,您需要使用其中一种方法
,或者
在这种情况下我更喜欢第二种方法,因为它涉及较少的比较。
接下来,您看起来就像刚刚开始接触 PHP。您没有关闭大括号,也没有使用 else 语句。
The code has lots of bugs-
Firstly,
returns an array with levels of all the users. You want to query specific user's level using WHERE clause.
Next , when you want to differentiate between <=5 and btween 5 &10, u need to use either of the methods
Or
I prefer the second one in this case as it involves one less comparison.
Next, you look like just beginning out in PHP. You did not close the braces and did not use else statements.