Jump to content

Issue with giving anchor tags a class based on its file name


Ricky55

Recommended Posts

Hi

 

Still very new to PHP but getting some good use out of it with includes.

 

I'm currently using this code to check the file name of the page and then give the corresponding anchor tag a class of active in order to style my main menu.

 

This is all working for most of my pages but my problem is that I now have multiple files called index.php one in the root of my site and some are in folders.

 

Is there any way around this issue? Can PHP check to see if its the index file in the root of the site or is there a better way. I suppose I could create a second variable in the index file in my root ie my home page and then check to say if the current page is called index and has the variable of home but how would I write this?

 

My code

 

$currentPage = basename($_SERVER['SCRIPT_NAME'], '.php');

<li><a href="/" <?php if ($currentPage == 'index') {echo 'class="active"';} ?>>Home</a></li>

Link to comment
Share on other sites

What I'd do is give each page a variable, let's say in each page you have something like

 

<?php
  $pageID = '1';
?>

 

But change the number for each page.

 

Then, for your navigation, you could have

 

<ul>
  <li><a href="#" <?php if ($pageID == '1') {echo 'class="active"';} ?>>Home</a></li>
  <li><a href="#" <?php if ($pageID == '2') {echo 'class="active"';} ?>>Link 2</a></li>
</ul>

 

Etc...

 

As far as I can see, this could work. I'm not sure if there are any problems with this method, or if it'll cause any problems within your site.

I hoe it works alright though.

Link to comment
Share on other sites

Cheers lads, just used the pageID, as I had a few I've created an array to hold these. Working a treat.

 

I know what I need to do in plain in english I just can't always turn that into PHP without some prompting.

 

Cheers

Link to comment
Share on other sites

I'm hoping this isn't too far off topic, but this article explains the security issues within PHP_Self.

 

Yep, that whole article talks about the dangers of using $_SERVER['PHP_SELF'] in the action attribute of a form tag. It doesn't say anything about using PHP_SELF elsewhere.

 

Using it to get the folder and filename for testing purposes should be just fine.

Link to comment
Share on other sites

Yep, that whole article talks about the dangers of using $_SERVER['PHP_SELF'] in the action attribute of a form tag. It doesn't say anything about using PHP_SELF elsewhere.

 

Using it to get the folder and filename for testing purposes should be just fine.

 

Oh right.

I've never really used PHP_SELF, jsut read a little bit about it.

Link to comment
Share on other sites

Cheers lads, just used the pageID, as I had a few I've created an array to hold these. Working a treat.

 

I know what I need to do in plain in english I just can't always turn that into PHP without some prompting.

 

Cheers

 

 

You could modify your original code using $_SERVER['PHP_SELF']:

 

<li><a href="/" <?php if ($_SERVER['PHP_SELF'] == '/index.php') { echo 'class="active"'; } ?>>Home</a></li>

Link to comment
Share on other sites

You could modify your original code using $_SERVER['PHP_SELF']:

 

<li><a href="/" <?php if ($_SERVER['PHP_SELF'] == '/index.php') { echo 'class="active"'; } ?>>Home</a></li>

 

Would that not cause problems in sub directories?

 

say the URL was

website.com/directory/index.php

 

The link to

website.com/index.php

would be active.

 

Correct me if I'm wrong.

Link to comment
Share on other sites

Would that not cause problems in sub directories?

 

say the URL was

website.com/directory/index.php

 

The link to

website.com/index.php

would be active.

 

Correct me if I'm wrong.

 

 

Nope, $_SERVER['PHP_SELF'] would contain a different value. So your test would be:

 

<li><a href="/" <?php if ($_SERVER['PHP_SELF'] == '/index.php') { echo 'class="active"'; } ?>>Home</a></li>
<li><a href="/directory/" <?php if ($_SERVER['PHP_SELF'] == '/directory/index.php') { echo 'class="active"'; } ?>>Directory</a></li>

Link to comment
Share on other sites

Nope, $_SERVER['PHP_SELF'] would contain a different value. So your test would be:

 

<li><a href="/" <?php if ($_SERVER['PHP_SELF'] == '/index.php') { echo 'class="active"'; } ?>>Home</a></li>
<li><a href="/directory/" <?php if ($_SERVER['PHP_SELF'] == '/directory/index.php') { echo 'class="active"'; } ?>>Directory</a></li>

 

Ah right. Cool.

 

That seems a lot easier than the $pageID method I used.

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.