Michelle Jackson
asked on
PHP if statement used to set class not functioning properly.
Hello, I am attempting to set the class of a <li> using a PHP if statement. The goal is to highlight the name of the person currently logged in. However the class is being set for all users and I cannot figure out why. Here is the code snippet:
I have verified that the $user_id is correct for the user that is logged in and that each line item does have a different UserID, what am I missing here? Thanks.
while ($row = mysql_fetch_array($leaders)) {?>
<li class ="<?php if($row["UserID"] = '$user_id'){echo 'leaderboard__user-highlight';} ?>"><div class="leader__wrap">
<div class="course-list__item--details"><h6><?php echo $row["FName"] ?> <?php echo $row["LName"] ?> </h6>
<p><?php echo $row["User_points"] ?></p>
<h5 class="user__badge badge--expert">Expert</h5>
</div>
</li>
<?php } ?>
I have verified that the $user_id is correct for the user that is logged in and that each line item does have a different UserID, what am I missing here? Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The = operator is assignment and I imagine you are wanting to carry out a check for equality. Please note that I also removed the ' characters from around your $user_id variable.
ASKER
Ah the extra quotation marks were the culprit...thanks.
It was both :) With the quotes around your variable, you would be comparing the value returned from the database to the string value '$user_id'. Glad that it is working for you now.
Recommendation - this might make your life a bit easier
First up take a read of how PHP strings work here.
In particular understand the difference between single and double quotes and how those work with respect to embedding variables.
Then take a look at the HEREDOC notation - it is a great tool when doing HTML block output as it allows for the best of both single and double quotes.
I have reformatted your code using HEREDOC and not only does it become easier to read it also shows that you are missing a closing </div> in your code which you will need to fix.
As mentioned above learn difference between == (test for equality) and = (assignment).
First up take a read of how PHP strings work here.
In particular understand the difference between single and double quotes and how those work with respect to embedding variables.
Then take a look at the HEREDOC notation - it is a great tool when doing HTML block output as it allows for the best of both single and double quotes.
I have reformatted your code using HEREDOC and not only does it become easier to read it also shows that you are missing a closing </div> in your code which you will need to fix.
As mentioned above learn difference between == (test for equality) and = (assignment).
<?php
while ($row = mysql_fetch_array($leaders)) {
// Create a class variable that holds the class value for use in the HEREDOC OUTPUT
// This uses a ternary expression for determining the value of $class
$class = $row["UserID"] == $user_id ? 'leaderboard__user-highlight' : '';
// OUTPUT HEREDOC WITH EMBEDDED VARIABLES. {} AROUND VARIABLES ARE OPTIONA
// BUT GOOD PRACTICE TO USE THEM.
echo <<< HTML
<li class="{$class}">
<div class="leader__wrap">
<div class="course-list__item--details">
<h6>{$row["FName"]} {$row["LName"]}</h6>
<p>{$row["User_points"}</p>
<h5 class="user__badge badge--expert">Expert</h5>
</div>
</div><!-- ADDED MISSING div -->
</li>
HTML;
}
ASKER
Julian, thanks, I actually did have the div tag there I just didn't include it in the code sample...thanks.
Ok, but still consider using HEREDOC - it really simplifies things quite a bit.
ASKER
Thanks Julian I definitely will.
ASKER