Link to home
Start Free TrialLog in
Avatar of burnedfaceless
burnedfaceless

asked on

Use jQuery or PHP to add an active class to a menu?

I will be making some changes to a menu. Because it is a lot of typing I just want to use the menu on a seperate PHP page and include that file on each page.

The active URL must be assigned a class of "active" in order for it to work. Is there a way to use jQuery especially, or PHP to detect the URL (my directories are folders and each folder may contain two or more pages for different devices) and assign the class to the relevant URL so I only have to type this once?



    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
     <div class="container">
       <div class="navbar-header">
         <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
           <span class="sr-only">Toggle navigation</span>
           <span class="icon-bar"></span>
           <span class="icon-bar"></span>
           <span class="icon-bar"></span>
         </button>
         <a class="navbar-brand" href = "../" >Consolidated Utilities</a>
       </div>
       <div class="collapse navbar-collapse">
         <ul class="nav navbar-nav">
           <li><a href="../about/">About Us</a></li>
           <li><a href = "../billing/">Billing</a></li>
		   <li><a href="../affiliatedsystems/">Affiliated Sytems</a><li>
           <li class = "active"><a href="../rates/">Rates</a></li>
           <li><a href="../conservation/">Conservation Efforts</a></li>
           <li><a href="../contact">Contact</a></li>
         </ul>
        </div><!--/.nav-collapse -->
      </div>
    </div>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of burnedfaceless
burnedfaceless

ASKER

Always a pleasure Ray.

I'm am just beginning to learn classes and object oriented programming in PHP. I've never learned classes before so this fits in nicely.
I have one question would you recommend this code in practice?

Or should I just do the menus by hand?
I would recommend it in practice.  It's possible to set up the class so it does not really need to be fully aware of all the possible links, and with a bit of generalization, it can handle new links as they are added to the script.

When you have a moment, make an E-E search for object oriented programming.  Our colleague @gr8gonzo has a couple of articles here about PHP object oriented programming that are really great foundations.
I will start object oriented tomorrow.

Thanks for responding.
There's a learning curve to object oriented code, and the semi-related matter of object-oriented design.  These are both new ways of thinking about application design and development.  They are the fruits of years of learning at the post-graduate level about computer science.  So, give yourself time to learn and practice - don't be impatient with yourself as you move into these concepts.  It will take time and experimentation.  The more you learn, the better your software will become - simpler, more elegant, reusable, testable, all things that make for quality and customer satisfaction.  It's a journey and a worthy expenditure of time and effort, I promise!
With your post in mind I have purchased Larry Ullmans object oriented PHP book.

I'm going to devote serious time to learning this because the source code has to be good and as you and the articles mention object oriented programming is glossed over.

If I don't have it enough after that I will purchase a denser book.

The articles were a great start and enlightening unfortunately to retain any of this I have to do a fair amount of work.

It seems like this is the last rudimentary programming concept I have to learn.
There are lots of good learning resources online, too.  But at the level of PHP notation, it may be simpler than it looks.

Array notation:
$arr['key'] => 'data_element';

Object notation:
$obj->key = 'data_element';

A reason to favor the object notation?  There are no required quotation marks, so it is easier to use the notation in other quoted strings with less risk of parse errors!