1

我在让我的简单 if 语句在 PHP while 循环中工作时遇到问题。这是我的代码:

$p=mysql_query("SELECT * FROM forum_posts WHERE forum_id='$cid' AND topic_id='$id' AND post_deleted='0' ORDER BY post_time ASC LIMIT  $offset, $rowsperpage");

while($post=mysql_fetch_assoc($p)){ 
    $userpost=$user->getUserData($post['userid']);

          if($userpost['specialmembership'] == 1){
            $pioneer = "True";
          }
echo $userpost['username'];
echo $pioneer;

}

所以,在上面的例子中,我有两个不同的用户。它们被命名为user1user2

  • 用户 1 具有特殊会员资格 = 0
  • User2 有特殊会员资格 = 1

上面的代码将输出以下内容(本例中的 while 循环循环了4 次):

它将输出:

user1

user2
true

user1
true

user2
true

问题就在这里,脚本在第三个循环中的 user1 上打印出 true。User1 不应该设置为 true,只有 user2 应该设置,因为他有specialmembership = 1;

这里有什么问题?

4

8 回答 8

5

你应该添加一个else

if($userpost['specialmembership'] == 1){
      $pioneer = "True";
}else {
     $pioneer = "False";
}

因为一旦$pioneer设置的值不会改变(如果你不再次这样做)

于 2013-10-23T15:00:46.380 回答
3

设置为 true 后您永远不会重置$pioneer,因此第一个“真实”用户之后的所有用户也将是真实的。你需要一个其他的:

      if($userpost['specialmembership'] == 1){
        $pioneer = "True";
      } else {
        $pioneer = "False";  // <---you need this
      }
于 2013-10-23T15:00:52.057 回答
3

尝试关注

$p=mysql_query("SELECT * FROM forum_posts WHERE forum_id='$cid' AND topic_id='$id' AND post_deleted='0' ORDER BY post_time ASC LIMIT  $offset, $rowsperpage");

while($post=mysql_fetch_assoc($p)){ 
    $userpost=$user->getUserData($post['userid']);

            $pioneer = "False";
          if($userpost['specialmembership'] == 1){
            $pioneer = "True";
          }
    echo $userpost['username'];
    echo $pioneer;
}
于 2013-10-23T15:00:52.943 回答
1

$pioneer正如 andrewsi 所说,每次进入循环时都应该重置变量。

while($post=mysql_fetch_assoc($p)){ 
  // defaults to 'False'
  $pioneer = 'False';
}

另一种选择是引入一个 else 分支:

else {
  $pioneer = 'False';
}

此外,您不应使用已弃用的 MySQL 扩展!切换到 MySQLi(使用准备好的语句)或 PDO!

于 2013-10-23T15:00:57.027 回答
1

您不会$pioneer在每个循环之后重置 的值。如果它不应该是,将其重置为空字符串"true"

while($post=mysql_fetch_assoc($p)){ 
    $userpost=$user->getUserData($post['userid']);
        if($userpost['specialmembership'] == 1){
            $pioneer = "true";
        }else{
            $pioneer = "";
    echo $userpost['username'];
    echo $pioneer;
}
于 2013-10-23T15:01:41.627 回答
1

$p=mysql_query("SELECT * FROM forum_posts WHERE forum_id='$cid' AND topic_id='$id'      

AND post_deleted='0' ORDER BY post_time ASC LIMIT  $offset, $rowsperpage");

while($post=mysql_fetch_assoc($p)){ 
$userpost=$user->getUserData($post['userid']);
      $pioneer="False";
      if($userpost['specialmembership'] == 1){
        $pioneer = "True";
      }
echo $userpost['username'];
echo $pioneer;

}
于 2013-10-23T15:01:58.247 回答
1

你永远不会重置你的$Pioneer变量

尝试

$pioneer = $userpost['specialmembership'] == 1 ? "True" : "False or Whatever you want";
于 2013-10-23T15:02:10.573 回答
0

尝试添加其他条件,例如

while($post=mysql_fetch_assoc($p)){ 
    $userpost=$user->getUserData($post['userid']);
          $pioneer = ($userpost['specialmembership'] == 1? "True": "False");
echo $userpost['username'];
echo $pioneer;
}

或在开头定义 $pioneer = "False" 并使用您的代码

while($post=mysql_fetch_assoc($p)){ 
$userpost=$user->getUserData($post['userid']);

      if($userpost['specialmembership'] == 1){
        $pioneer = "True";
      }
echo $userpost['username'];
echo $pioneer;

}
于 2017-05-17T16:40:56.557 回答