Jump to content

php set current page "active"


WatsonN

Recommended Posts

I have a menu on my site on each page, I want to put it in it's own menu.php file but i'm not sure how to set the class="active" for whatever page i'm on.

Here is my code:

<?php
if ($url == $curpage) {
$class = "active";
} else {
$class = "in-active";
}
?>
            <ul id="top-navigation">
                <li><a href="./" class="<? echo $class ?>">Homepage</a></li>
                <li><a href="?p=MM" class="<? echo $class ?>">Maintenance Mode</a></li>
                <li><a href="?p=UCP" class="<? echo $class ?>">User CP</a></li>
                <li><a href="?p=LYRICS" class="<? echo $class ?>">Lyrics</a></li>
                <li><a href="?p=SB1" class="<? echo $class ?>">Sidebar 1</a></li>
                <li><a href="?p=SB2" class="<? echo $class ?>">Sidebar2</a></li>
                <li><a href="?p=SHP" class="<? echo $class ?>">Site HomePage</a></li>
                <li><a href="?p=EDTpst" class="<? echo $class ?>">post.php</a></li>
                <li><a href="?p=EDTUCP" class="<? echo $class ?>">UCP.php</a></li>
                <li><a href="?p=EDTtimer" class="<? echo $class ?>">timer.php</a></li>
            </ul>

I want $curpage to be the only page I'm on not all of the pages.

How would I do this would it be like

$url = (isset($_GET['p']) ? $_GET['p'] : 'home');
$s1 = "main" ;
$s2 = "MM" ;
$s3 = "UCP" ;
$s4 = "LYRICS" ;
//cont. . . 
if ($url == $s1) {
    $class = "active";
} elseif ($url == $s2) {
    $class = "active";
} elseif ($url == $s3) {
    $class = "active";
} elseif ($url == $s4){
$class = "active";
}
//cont. . . 

Link to comment
Share on other sites

Hmm I think that would only effect the class which could be nice for css but would not cause it to display the element as a whole or not. I am not sure and I haven't tested it but while typing it, it sounds logical ;) Forgive my ignorance btw, I wasn't totally sure what you like certainly the sentence before the second code is unclear for me.

 

btw don't forget the semicolon in the echo's i.e.:

<? echo $class ?>

 

-edit oh i think i understand what you mean now, and its css related. I think you doing it right than

also, for some settings,  short tags seem to be causing trouble:  <?php ?> is preferred

 

 

 

 

Link to comment
Share on other sites

Yes Its CSS when the active tag it there it causes the tab to be highlighted and the rest to not be.

I fixed it for now its bad but it works:

Menu.php

            <ul id="top-navigation">
                <li><a href="./" class="<?php if ($url == $curpage) {echo "active";} else {echo "in-active";} ?>">Homepage</a></li>
                <li><a href="?p=MM" class="<?php if ($url == $s2) {echo "active";} else {echo "in-active";} ?>">Maintenance Mode</a></li>
                <li><a href="?p=UCP" class="<?php if ($url == $s3) {echo "active";} else {echo "in-active";} ?>">User CP</a></li>
                <li><a href="?p=LYRICS" class="<?php if ($url == $s4) {echo "active";} else {echo "in-active";} ?>">Lyrics</a></li>
                <li><a href="?p=SB1" class="<?php if ($url == $s5) {echo "active";} else {echo "in-active";} ?>">Sidebar 1</a></li>
                <li><a href="?p=SB2" class="<?php if ($url == $s6) {echo "active";} else {echo "in-active";} ?>">Sidebar2</a></li>
                <li><a href="?p=SHP" class="<?php if ($url == $s7) {echo "active";} else {echo "in-active";} ?>">Site HomePage</a></li>
                <li><a href="?p=EDTpst" class="<?php if ($url == $s9) {echo "active";} else {echo "in-active";} ?>">post.php</a></li>
                <li><a href="?p=EDTUCP" class="<?php if ($url == $s10) {echo "active";} else {echo "in-active";} ?>">UCP.php</a></li>
                <li><a href="?p=EDTtimer" class="<?php if ($url == $s11) {echo "active";} else {echo "in-active";} ?>">timer.php</a></li>
            </ul>

 

vars

$s1 = "main" ;
$s2 = "MM" ;
$s3 = "UCP" ;
$s4 = "LYRICS" ;
$s5 = "SB1" ;
$s6 = "SB2" ;
$s7 = "SHP" ;
$s8 = "login" ;
$s9 = "EDTpst" ;
$s10 = "EDTUCP" ;
$s11 = "EDTtimer" ;
$home = "main.php" ;
$l1 = "main.php" ;
$l2 = "MM.php" ;
$l3 = "UCP.php" ;
$l4 = "LYRICS.php" ;
$l5 = "SB1.php" ;
$l6 = "SB2.php" ;
$l7 = "SHP.php" ; 
$l8 = "login.php" ;
$l9 = "EDTP.php" ;
$l10 = "EDTUCP.php" ;
$l11 = "EDTtmr.php" ;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.