php if 语句用于显示某些 html 块

发布于 2024-11-05 03:19:10 字数 321 浏览 3 评论 0原文

我目前正在尝试检查我的数据库表以查看用户的级别是否等于或小于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情绪 2024-11-12 03:19:10

试试这个:

<?php if ($levels <= 5): ?>

html list

<?php elseif ($levels <= 10): ?>

html list

<?php endif; ?>

正如David Fells 所说,确保您首先得到结果:)

编辑:我认为我误读了您的问题。试试这个:

<?php if ($levels <= 10): /* If equal to or less than 10 */ ?>

    <?php if ($levels > 5): /* if greater than 5 */ ?>

    first part of html list

    <?php endif; ?>

second part of html list

<?php endif; ?>

我对此感到困惑:

如果用户级别等于或小于 10,则显示整个 html 列表,如果不是且等于或小于 5 仅显示列表的剩余部分

..as 0-5 将小于或等于 10。请原谅混淆,GL!

Try this:

<?php if ($levels <= 5): ?>

html list

<?php elseif ($levels <= 10): ?>

html list

<?php endif; ?>

As David Fells said, make sure you are getting a result first :)

EDIT: I misread your question I think. Try this instead:

<?php if ($levels <= 10): /* If equal to or less than 10 */ ?>

    <?php if ($levels > 5): /* if greater than 5 */ ?>

    first part of html list

    <?php endif; ?>

second part of html list

<?php endif; ?>

I was confused by this:

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

..as 0-5 will be less or equal to ten. Please excuse the mixup, GL!

溺ぐ爱和你が 2024-11-12 03:19:10
$result = mysql_query(sprintf("SELECT level FROM users WHERE user_id = %d", $current_user_id));
if (list($level) = mysql_fetch_array($result)) {
  if ($level <= 5) { 
    // whatever
  }
  elseif ($level <= 10) {
    // whatever
  }
  else {
    // whatever
  }
}

这样就完全清楚了吗?您需要提供当前用户的 ID,然后需要实际从 $result 中检索值。 $result 是资源类型的变量,您必须使用专门设计的函数来处理它。

$result = mysql_query(sprintf("SELECT level FROM users WHERE user_id = %d", $current_user_id));
if (list($level) = mysql_fetch_array($result)) {
  if ($level <= 5) { 
    // whatever
  }
  elseif ($level <= 10) {
    // whatever
  }
  else {
    // whatever
  }
}

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.

小帐篷 2024-11-12 03:19:10

您需要在级别 <= 10 之前询问级别 <= 5,因为级别 <= 10 总是会找到级别 <= 5。

You need to ask for the levels <= 5 before levels <= 10, because levels <= 10 will always find levels <= 5.

沉睡月亮 2024-11-12 03:19:10

该代码有很多错误

- 首先,

$levels = mysql_query("SELECT level FROM users");

返回一个包含所有用户级别的数组。您想使用 WHERE 子句查询特定用户的级别。

接下来,当您想要区分 <=5 和 5 &10 之间时,您需要使用其中一种方法

if($levels>5 && $levels <=10){
      `enter code here`
}elseif($levels<=5){
      `enter code here`
}

,或者

if($levels<=5){
      `enter code here`
}elseif($levels<=10){
      `enter code here`
}

在这种情况下我更喜欢第二种方法,因为它涉及较少的比较。

接下来,您看起来就像刚刚开始接触 PHP。您没有关闭大括号,也没有使用 else 语句。

The code has lots of bugs-

Firstly,

$levels = mysql_query("SELECT level FROM users");

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

if($levels>5 && $levels <=10){
      `enter code here`
}elseif($levels<=5){
      `enter code here`
}

Or

if($levels<=5){
      `enter code here`
}elseif($levels<=10){
      `enter code here`
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文