1

I'm having problems getting my simple if-statement to work inside a PHP while loop. This is my code:

$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;

}

So, in the above example I have two different users. They are named user1 and user2.

  • User1 has specialmembership = 0
  • User2 has specialmembership = 1

The above code will output the following (the while loop in this example is getting looped 4 times):

It will output:

user1

user2
true

user1
true

user2
true

The problem is here, that the script is printing out true on user1 in the third loop. User1 shouldn't be set to true, only user2 should that, since he has specialmembership = 1;

What is wrong here?

1
  • 2
    Reset the value of $pioneer to false as the first line inside your while loop. Commented Oct 23, 2013 at 15:00

8 Answers 8

5

You should add an else

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

Because once the value of $pioneer is set does not change (if you don't do it again)

Sign up to request clarification or add additional context in comments.

Comments

3

You never reset $pioneer after it's set to true, so ALL users after the first 'true' user will also be true. You need an else:

      if($userpost['specialmembership'] == 1){
        $pioneer = "True";
      } else {
        $pioneer = "False";  // <---you need this
      }

Comments

3

Try following

$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;
}

Comments

1

As andrewsi said, you should reset you $pioneer variable each time you enter the loop.

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

Another Option would be to introduce an else branch:

else {
  $pioneer = 'False';
}

Also, you should NOT use the deprecated MySQL extension anymore! Switch to MySQLi (with prepared Statements) or PDO!

Comments

1

You're not resetting the value of $pioneer after each loop. Reset it to an empty string if it's not supposed to be "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;
}

Comments

1

write

$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;

}

Comments

1

You never reset your $Pioneer Variable

try

$pioneer = $userpost['specialmembership'] == 1 ? "True" : "False or Whatever you want";

Comments

0

Try adding else condition like

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

or define $pioneer = "False" in the beginning and use your code

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

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.