How to make if condition in HTML sidebar in PHP

Issue

I have a sidebar as follows in HTML,

<div class="sidebar">
    <h2 style="font-family: Verdana;">Dashboard</h2>
    <ul>
        <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
        
        <li><a href="dummy1.php"><i class="fa fa-paper-plane"></i> dummy1</a></li>
        <li><a href="dummy2.php"><i class="fa fa-mobile"></i> dummy2</a></li>
        <li><a href="dummy3.php"><i class="fa fa-phone"></i> dummy3</a></li>
        <li><a href="dummy4.php"><i class="fa fa-plug"></i> dummy4</a></li>
        
    </ul> 
    
  </div>

I need to check the username and show the tab.By using below code part I could get the current user logged in. I need to check whether below value is equal to John if and only if I need to show the tabs.

<?php echo htmlspecialchars($_SESSION["username"]); ?>

As an example,

if( htmlspecialchars($_SESSION["username"])=='John'){
             <li><a href="dummy1.php"><i class="fa fa-paper-plane"></i> dummy1</a></li>
            <li><a href="dummy2.php"><i class="fa fa-mobile"></i> dummy2</a></li>
            <li><a href="dummy3.php"><i class="fa fa-phone"></i> dummy3</a></li>
            <li><a href="dummy4.php"><i class="fa fa-plug"></i> dummy4</a></li>
}

Can someone show me how to achieve this with php and HTML?

Solution

You’re nearly there – just wrap the PHP bits in PHP tags.

Also as an aside, you don’t need htmlspecialchars() unless you’re outputting the value into a HTML document. When you’re just using it in an if, you’re not outputting it.

<?php 
if( $_SESSION["username"] =='John'){
?>
  <li><a href="dummy1.php"><i class="fa fa-paper-plane"></i> dummy1</a></li>
  <li><a href="dummy2.php"><i class="fa fa-mobile"></i> dummy2</a></li>
  <li><a href="dummy3.php"><i class="fa fa-phone"></i> dummy3</a></li>
  <li><a href="dummy4.php"><i class="fa fa-plug"></i> dummy4</a></li>
<?php
}
?>

Answered By – ADyson

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.