Anon
Anon

Reputation: 865

Setting tabs as active using PHP

I'm setting the tabs on my site and finding which ones are active using PHP. Here's my code from the header file. The tab is set in the other pages and sent to the header.

<ul class="nav pull-left">  

<?php 
    if($_SESSION['val']==="1")
    {
                            echo '<li' . ($tab == '1' ? 'class="active"' : '') . '><a href="1.php">1</a></li>';
                            echo '<li' . ($tab == '2' ? 'class="active"' : '') . '><a href="2.php">2</a></li>';
                            echo '<li' . ($tab == '3' ? 'class="active"' : '') . '><a href="3.php">3</a></li>';
                            echo '<li' . ($tab == '4' ? 'class="active"' : '') . '><a href="4.php">4</a></li>';
    }    
?>
</ul>

I'm able to see which tab is active and the tabs are set as required but the list items appear as a list and without the navbar styling as setting a active tab is causing a clash. In pages where none of these tabs are active I am able to see the tabs in their normal navbar styling. Is there a better way for me to do this?

UPDATE

Correction: The tabs are not displayed as active at all. But if I click on the 4th tab then the required styling appears.

Upvotes: 0

Views: 3159

Answers (2)

Bastian Doeen
Bastian Doeen

Reputation: 128

If your text is exact, you might just miss a blank between

li

and

class

resulting in

<liclass

which is not html anymore.

Upvotes: 1

matewka
matewka

Reputation: 10148

You forgot to add spaces before the class word

echo '<li' . ($tab == '1' ? ' class="active"' : '') . '><a href="1.php">1</a></li>';
echo '<li' . ($tab == '2' ? ' class="active"' : '') . '><a href="2.php">2</a></li>';
echo '<li' . ($tab == '3' ? ' class="active"' : '') . '><a href="3.php">3</a></li>';
echo '<li' . ($tab == '4' ? ' class="active"' : '') . '><a href="4.php">4</a></li>';

Upvotes: 2

Related Questions