1

I want to add class current for every li when it is active using php (I am absolutely new with it). I have the following code:

<ul id="frumos">
    <li {if $pagename eq "upcoming"} class="current" {/if}><a href="/upcoming.php?category=economic">Economic</a></li>  
   <li {if $pagename eq "/upcoming.php?category=other"} *(this doesn't work)* class="current" {/if}><a href="/upcoming.php?category=other">Freestyle</a></li>

   <li><a href="/upcoming.php?category=social">Social</a></li>
</ul>

If i use {if $pagename eq "upcoming"} it works fine but if I put {if $pagename eq "/upcoming.php?category=other"} it doesn't work what structure should I use in this case?

Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
Andrew Ceban
  • 118
  • 15

2 Answers2

1

Try this:

<?PHP
$category = $_REQUEST[category];

echo "<ul id='frumos'>";
    echo "<li ";
    if($category == 'economic'){
        echo " class='current'";
    }
    echo "><a href='/upcoming.php?category=economic'>Economic</a></li> ";
    echo "<li";
    if($category == 'other'){
        echo " class='current'";
    }
    echo "><a href='/upcoming.php?category=other'>Freestyle</a></li>";
    echo "<li";
    if($category == 'social'){
        echo " class='current'";
    }
    echo "><a href='/upcoming.php?category=social'>Social</a></li>";
echo "</ul>";
?>
Rob
  • 50
  • 1
  • 1
  • 10
0

The easiest and cleanest way to butcher your html is with a turinary. Essentially it is a compact if else statment.

<li <?php echo ($pagename === "/upcoming.php?category=other") ? echo 'class="current"' : ''; ?> >
David
  • 253
  • 1
  • 2
  • 10