We help IT Professionals succeed at work.

PHP if statement used to set class not functioning properly.

Michelle Jackson
on
76 Views
1 Endorsement
Last Modified: 2018-09-20
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

Software Developer / Linux System Administrator
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

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
CERTIFIED EXPERT

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.

Author

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

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.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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

Author

Commented:
Thanks Julian I definitely will.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.