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


if statement - php if statment for showing certain blocks of html

我目前正在检查我的db表,看看用户的级别是否等于或小于10,如果是,则显示整个html列表,如果不是,并且等于或小于5,则只显示列表的其余部分。这是我开始的地方:

<?php
$levels = mysql_query("SELECT level FROM users");
  if ($levels <= 10) {
?>
html list
<?php 
if ($levels <= 5)
?>
html list
<?php
}
?>

我正试图把我的头缠在它周围,我似乎就是拿不到它。

试试这个:

<?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,则仅显示列表的剩余部分

因为0-5将小于或等于10。请原谅我搞砸了,GL!

$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是一个资源类型的变量,您必须使用为此设计的函数来处理它

您需要询问级别<=5级之前<=10,因为级别<=10将始终找到级别<=5.

代码有很多错误-

首先,

$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语句。