0

I'm currently checking the users current page, and the applying a .css class to the correct menu item.

My question is, how can I do this best? Currently, I have this:

    $currentpage = $_SERVER['PHP_SELF'];
  if($currentpage == "/prefs.php") $active = "active";
  if($currentpage == "/acc.php") $active = "active";
  if($currentpage == "/forum.php") $active = "active";

<a href='acc.php' title='Sync' aria-hidden='true' class='{$active} tipTip'>

<a href='acc.php' title='Sync' aria-hidden='true' class='{$active} tipTip'>
<a href='forum.php' title='Statistics' aria-hidden='true' class='{$active} tipTip'>

But this will just add the active class to all the anchor elements.

4
  • I would just do this with client side scripting, like jQuery. No need to server side scripting for a task like this. Commented Sep 4, 2013 at 17:55
  • I like using arrays: if(in_array($currentpage,["/prefs.php","/acc.php","/forum.php"])) $active="active"; Commented Sep 4, 2013 at 17:55
  • Unfortunatly that may be your best bet if you need to do it with php. Unless you want to post more code to let us know the requirements. (how many pages, how many links, etc.). jQuery might be your best bet for this. Commented Sep 4, 2013 at 17:55
  • Have you tried using jquery passing an "active" state via ajax? Commented Sep 4, 2013 at 18:00

5 Answers 5

4

The simple way would be to add the if statement inline with your HTML:

<?php $currentpage = $_SERVER['PHP_SELF']; ?>

<a href='acc.php' title='Sync' aria-hidden='true' class='<?php if($currentpage == "/prefs.php") echo "active "; ?>tipTip'>
<a href='acc.php' title='Sync' aria-hidden='true' class='<?php if($currentpage == "/acc.php") echo "active "; ?>tipTip'>
<a href='forum.php' title='Statistics' aria-hidden='true' class='<?php if($currentpage == "/forum.php") echo "active "; ?>tipTip'>

Though this is potentially less readable than other options.

Sign up to request clarification or add additional context in comments.

3 Comments

I would avoid mixing php and html it like this
@TheApptracker You can't avoid mixing php with either html or js in order to solve this problem.
Of course there are cleaner ways of inserting php variables into HTML. If the page is subject to change later on, it would make sense to set up a more dynamic method of applying styles.
2

Give different names to your variables:

$currentpage = $_SERVER['PHP_SELF'];

if($currentpage == "/prefs.php") $active1 = "active";
if($currentpage == "/acc.php") $active2 = "active";
if($currentpage == "/forum.php") $active3 = "active";

<a href='acc.php' title='Sync' aria-hidden='true' class='{$active1} tipTip'>
<a href='acc.php' title='Sync' aria-hidden='true' class='{$active2} tipTip'>
<a href='forum.php' title='Statistics' aria-hidden='true' class='{$active3} tipTip'>

3 Comments

+1 to offset because this solves the problem, however, it has the same syntax issue as the question: {$active} is for PHP string interpolation, it's not going to work in the HTML
Also, it may make sense to use meaningful variable names, such as $activePrefs, $activeACC ...
I'm just suggesting a solution...not here to define the variables for him :) ... and the OP doesn't talk about syntax issue...just logic issue...may be the HTML code is in a PHP variable between double quote and the OP just didn't provide it this way...who knows :)
1
<?php
$currentpage = $_SERVER['PHP_SELF'];

echo "<a href='prefs.php' title='Sync' aria-hidden='true' class='" . ($currentpage == '/prefs.php' ? 'active' : '') . " tipTip'>
      <a href='acc.php' title='Sync' aria-hidden='true' class='" . ($currentpage == '/acc.php' ? 'active' : '') . " tipTip'>
      ...";

If you don't like this, you could also use a switch() { case '': break; } setup.

Comments

0

Get Self Function:

function getSelf($dir=""){
    if($dir==""){ $dir = $_SERVER['PHP_SELF']; }
    $self = explode("/",$dir);
    return $self[count($self)-1];
}

and then something like a delimiter for your menu:

<?php
                    $cls="";
                    $quick = "index.php||other_page.php||other_page2.php";
                    $stack = explode("||",$quick);
                    if(in_array(getSelf(), $stack)){ $cls = " active"; } else { $cls = ""; }
                ?>

Then class="<?php echo $cls; ?>"

1 Comment

short_open_tags are off by default. Use proper <?php /***/ ?>
0

There's a slightly different approach, but it requires JavaScript as well (I know you didn't list JavaScript in tags, but still you might find this useful). The trick is to make page names respond to (modified) DOM element IDs or classes.

// or just make a simple function that strips the name from the $currentpage
// if the names are consistent like in your example
if ($currentpage == "/prefs.php") $id="prefs"; 
elseif ($currentpage == "/acc.php") $id="acc";
elseif ($currentpage == "/forum.php") $id="forum"; 

Then, make the JavaScript call to find the proper anchor element and assign the "active" class to it. Here's a jQuery example:

var active = '<?php echo $id; ?>';
$('a .' + active).addClass('active'); // for class name
$('#' + active).addClass('active'); // for id, you get the point

This is just a bare bone example, but I think it might be helpful and make your code more readable if you use JavaScript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.