PHP if statement used to set class not functioning properly.

Michelle Jackson
Michelle Jackson used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Developer / Linux System Administrator / Managing Director
Commented:
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

Author

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.
Martyn SpencerSoftware Developer / Linux System Administrator / Managing Director

Commented:
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.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
Ah the extra quotation marks were the culprit...thanks.
Martyn SpencerSoftware Developer / Linux System Administrator / Managing Director

Commented:
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.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

Author

Commented:
Julian, thanks, I actually did have the div tag there I just didn't include it in the code sample...thanks.
Most Valuable Expert 2017
Distinguished Expert 2018

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

Author

Commented:
Thanks Julian I definitely will.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial