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>
							</div>
						</li>
					<?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.
Michelle JacksonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martyn SpencerSoftware Developer / Linux System Administrator / Managing DirectorCommented:
Did you intend to use
$row["UserID"] = '$user_id'

Open in new window

or did you mean
$row["UserID"] == $user_id

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michelle JacksonAuthor Commented:
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.
0
Martyn SpencerSoftware Developer / Linux System Administrator / Managing DirectorCommented:
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.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Michelle JacksonAuthor Commented:
Ah the extra quotation marks were the culprit...thanks.
0
Martyn SpencerSoftware Developer / Linux System Administrator / Managing DirectorCommented:
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.
0
Julian HansenCommented:
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).

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

Open in new window

0
Michelle JacksonAuthor Commented:
Julian, thanks, I actually did have the div tag there I just didn't include it in the code sample...thanks.
0
Julian HansenCommented:
Ok, but still consider using HEREDOC - it really simplifies things quite a bit.
0
Michelle JacksonAuthor Commented:
Thanks Julian I definitely will.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.