Avatar of Michelle Jackson
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:
	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>
					<?php } ?>

Open in new window

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.

Avatar of undefined
Last Comment
Michelle Jackson

8/22/2022 - Mon
Martyn Spencer

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Michelle Jackson

I used == got no hits and then I played around with it and changed it to = and that is when all rows were assigned the class.
Martyn Spencer

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.
Michelle Jackson

Ah the extra quotation marks were the culprit...thanks.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Martyn Spencer

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.
Julian Hansen

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).

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' : '';

echo <<< HTML
<li class="{$class}">
  <div class="leader__wrap">
    <div class="course-list__item--details">
      <h6>{$row["FName"]} {$row["LName"]}</h6>
      <h5 class="user__badge  badge--expert">Expert</h5>
  </div><!-- ADDED MISSING div -->

Open in new window

Michelle Jackson

Julian, thanks, I actually did have the div tag there I just didn't include it in the code sample...thanks.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Julian Hansen

Ok, but still consider using HEREDOC - it really simplifies things quite a bit.
Michelle Jackson

Thanks Julian I definitely will.