Reputation: 6052
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
.
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?
Upvotes: 1
Views: 198
Reputation: 248
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;
}
Upvotes: 0
Reputation: 4197
You never reset your $Pioneer
Variable
try
$pioneer = $userpost['specialmembership'] == 1 ? "True" : "False or Whatever you want";
Upvotes: 1
Reputation: 669
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;
}
Upvotes: 1
Reputation: 18891
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;
}
Upvotes: 1
Reputation: 29424
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!
Upvotes: 1
Reputation: 1289
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;
}
Upvotes: 3
Reputation: 360572
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
}
Upvotes: 3
Reputation: 16458
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)
Upvotes: 5