0

I see a lot of these kinds of questions around this website, and on the web, but I can't find the specific answer to what I'm trying to do. I don't want to use JS, I want to use it as little as possible, but if I have to I'll resort to it.

I want to use PHP to add an active class to my nav links when that page is open, or if a page in the drop down menu (the sub-page's I was referring to) are open. Here is my code.

In all of my pages I have "included" the following:

<?php include('includes/header.php'); ?>

As part of that included page I have the following:

<ul class="firstbar">
    <li class="item"><a href="index.php">Home</a></li>
    <li class="item has-dropdown">
        <a href="about.php">About Us</a>
        <ul class="dropdown">
        <li><a href="about.php#about">G2 Drivers</a></li>
        <li><a href="about.php#locations">Locations</a></li>
        <li><a href="about.php#instructors">Instructors</a></li>
        </ul>
    </li>
    <li class="item has-dropdown">
        <a href="services.php">Services</a>
        <ul class="dropdown">
        <li><label>In-car</label></li>
        <li><a href="services/defensivedriving.php">Defensive Driving</a></li>
        <li><a href="services/incarlessons.php">In-car Lessons</a></li>
        <li class="divider"></li>
        <li><label>In-class</label></li>
        <li><a href="services/curriculum.php">Curriculum</a></li>
        </ul>
    </li>
</ul>

You can take a look at the test url here: www.mrobertsdesign.ca/g2drivers/ (the active class is already available, just not sure how to go about adding it)

The active class needs to be added to the list item tag right before the anchor link tag, like the following:

    <li class="active item"><a href="index.php">Home</a></li>

The about.php page has a dropdown menu but it's only hash-linked to parts of that page. But for pages like services.php which has a drop down to different pages, I want the 'Services' list item tag to have the active class as well, so nothing different. Like the following:

    <li class="active item has-dropdown">
        <a href="services.php">Services</a>
        <ul class="dropdown">
        <li><label>In-car</label></li>
        <li><a href="services/defensivedriving.php">Defensive Driving</a></li>
        <li><a href="services/incarlessons.php">In-car Lessons</a></li>
        <li class="divider"></li>
        <li><label>In-class</label></li>
        <li><a href="services/curriculum.php">Curriculum</a></li>
        </ul>
    </li>

The thing that I can't find in any of the questions that I've looked at so far is if one were to be currently on one of the pages in the drop down menu, most of the questions don't seem to include the idea of a drop down menu and the code usually includes finding the current page, etc, so there's no way so far for me to use that for this problem.

All help is appreciated! Thanks!


@1andsock answered the first half of the question below, but I have a couple pages that are in different folders and have the same name so the script doesn't work in those situations. For anyone who wished to take a look and would like to help me out with that, that would be awesome =)

Add class="active" to active page using PHP

That is a similar article to this one, that I just saw, and it seems to answer what I'm looking for, but it has a 'noactive' class, I just want it to be 'active' or just not display active.

Community
  • 1
  • 1
mattroberts33
  • 260
  • 5
  • 19
  • And your programming question is? If you're looking on how to parse and process HTML with PHP, here is a reference question that should give some pointers: [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – hakre Apr 27 '14 at 23:57
  • Hey, thanks for the feedback and providing a link! Also thanks for pointing out my lack of clarity in terms of an actual question lol, I guess I didn't really know what I was looking for... But rest assured I'm just not very good with PHP, I actually meant to post a question asked by someone else I stumbled upon with a similar question (not similar enough though), but I believe I've found the answer so far below! – mattroberts33 Apr 28 '14 at 01:49
  • Do you generate the HTML with PHP or is this HTML already? – hakre Apr 28 '14 at 01:52
  • 1
    I would do that with DOMDocument and DOMXpath then, however I can not provide an example right away. But there are existing examples on the site already, keywords are PHP, HTML, DOMDOCUMENT, XPATH. – hakre Apr 28 '14 at 08:32
  • Thanks! I'll look around this this, but if you could also give me an example later that would be awesome! – mattroberts33 Apr 28 '14 at 09:31

2 Answers2

2

I haven't tested this, but why can't you just do:

<?php
  $current_page = basename($_SERVER['PHP_SELF']);
?>

and then:

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

for normal links, and:

<li class="<?php if ($current_page == "index.php"){ echo "active "; }?> item has-dropdown">
    <a href="about.php">About Us</a>

for links with dropdowns?

1andsock
  • 1,557
  • 10
  • 15
  • I love this answer, thanks! I'll put it together when I get home again tonight =) I was thinking of something a little more like a script at the bottom of the page, for whatever reason, but this looks a lot better! I guess I'm too used to working with jQuery now lol – mattroberts33 Apr 28 '14 at 01:44
  • The drop down example you gave is the same thing as the first example. It would have to be an array that lists the pages that could spark the active class? – mattroberts33 Apr 28 '14 at 09:34
  • No it could something like: if ($current_page == "about.php" || $current_page == "services/incarlessons.php") and so on – 1andsock Apr 28 '14 at 21:03
  • It pretty much works, just one thing now! I have a Courses page and a Packages page, and they link to courses/oshawa.php and packages/oshawa.php for example, with the code you gave me I have to write $current_page == "oshawa.php", I can't include that folder. How can we fix this? Maybe there's a way to force the script to start at root so we have to add the folder? – mattroberts33 Apr 28 '14 at 22:17
  • I don't mean to bug but do you have any ideas for that last issue in this question? @1andsock – mattroberts33 Apr 29 '14 at 20:31
  • @mattroberts33, You could use something like `$current_page_folder = dirname($_SERVER['PHP_SELF']);` and then just add it to the if statement so `if ($current_page == "oshawa.php" && $current_page_folder == "packages")` – 1andsock Apr 30 '14 at 03:47
  • Could you maybe update your example? @1andsock Just so I do it right =p – mattroberts33 Apr 30 '14 at 04:05
  • This doesn't seem to work @1andsock updated it, on www.mrobertsdesign.ca/courses/oshawa.php – mattroberts33 Apr 30 '14 at 04:29
1

If you are using sessions, you could set a current page session variable and use it on the page:

On each page:

session_start();
$_SESSION['current_page'] = 'this_page';

Then on your menu page:

<li class="<?php if ($_SESSION['current_page'] == 'this_page'){ echo 'active '; }?> item"><a href="index.php">Home</a></li>
Andrew
  • 18,680
  • 13
  • 103
  • 118