Home > Enterprise >  How to make if condition in HTML sidebar in PHP
How to make if condition in HTML sidebar in PHP

Time:06-10

I have a sidebar as follows in HTML,

<div >
    <h2 style="font-family: Verdana;">Dashboard</h2>
    <ul>
        <li><a href="#"><i ></i> Home</a></li>
        
        <li><a href="dummy1.php"><i ></i> dummy1</a></li>
        <li><a href="dummy2.php"><i ></i> dummy2</a></li>
        <li><a href="dummy3.php"><i ></i> dummy3</a></li>
        <li><a href="dummy4.php"><i ></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 ></i> dummy1</a></li>
            <li><a href="dummy2.php"><i ></i> dummy2</a></li>
            <li><a href="dummy3.php"><i ></i> dummy3</a></li>
            <li><a href="dummy4.php"><i ></i> dummy4</a></li>
}

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

CodePudding user response:

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 ></i> dummy1</a></li>
  <li><a href="dummy2.php"><i ></i> dummy2</a></li>
  <li><a href="dummy3.php"><i ></i> dummy3</a></li>
  <li><a href="dummy4.php"><i ></i> dummy4</a></li>
<?php
}
?>
  • Related