Page 1 of 1

Selecting class

Posted: Wed Apr 27, 2011 9:35 pm
by matthew.mcdougall
Hey Guys,

Stupid question - I am wanting to apply the "current" class to a navigation item, however, I need this to apply to a number of pages. For example, when someone is at either page1.php or page 2.php, I need the navigation item to be current.

This is what I have, however, the class is applying to page3.php (the nav) no matter what page you are on.

Code: Select all

<li><a <?php if ($page == 'page1.php' or 'page2.php') { ?> class="current" <? php } ?> href="page3.php">Page 3</a></li>
Any help would be greatly appreciated!

Thanks,
Matt

Re: Selecting class

Posted: Thu Apr 28, 2011 2:01 am
by social_experiment

Code: Select all

<?php
function applyClass($pageName) {
 // array containing pages that needs the 'current' class assigned to them.
 $currentPageArray = array('page3', 'page5');

 if ($pageName != '') {
  foreach ($currentPageArray as $value) {
   if ($pageName == $value) {
    $className = 'current';
    return $className;
   }
   else {
    $className = 'other';
    return $className;
   }
  }
 }
}
// how to use the function
<li><a class="<?php echo applyClass('page1'); ?>" href="page1.php">Page 1</a></li>
?>
If you want to apply the class to each item in the list, this function might help.

Re: Selecting class

Posted: Thu Apr 28, 2011 6:50 am
by matthew.mcdougall
Thanks for that, I see what you are getting at, but what I need to do is just apply the current class to a link (the nav) but i need multiple pages to control this. So when on either page 1 or 2, the nav menu item will have the class applied to it, and when on page 3 or 4, a different nav item will have the class. I can do this with a single page, as you see in the original, but as soon as I add the 'or' to the code to add another page, it does not work.

Any other ideas?

Thanks,
Matt

Re: Selecting class

Posted: Thu Apr 28, 2011 2:04 pm
by social_experiment
matthew.mcdougall wrote:but what I need to do is just apply the current class to a link (the nav) but i need multiple pages to control this. So when on either page 1 or 2, the nav menu item will have the class applied to it, and when on page 3 or 4, a different nav item will have the class.
I don't quite follow what you have in mind here :)

Re: Selecting class

Posted: Thu Apr 28, 2011 6:11 pm
by matthew.mcdougall
Sorry. Ok, I have a navigation bar with drop down menus. The main navigation bars each have a current class associated with them. What I want to do, Is have each page in the drop down under the main navigation, apply the current class to its associated main navigation item. So Under the services main nav item, there are pages 1, 2, 3, and 4; and under the products main nav item, there are pages 4, 5, 6. I need code for the header file, so that whenever you are on page 1, 2 3, or 4, the current class applies to the services main item nav, and whenever you are on pages 4, 5, or 6, it applies to the product main item nav.

The following code applies the current class to the services item nav, when you are on carpetcleaning.php, for example.

Code: Select all

<li><a <?php if ($page == 'carpetcleaning.php') { ?> class="current" <? php } ?> href="services.php">services</a></li>
What I need is to be able to apply this to multiple pages, so I thought I could use 'or' do do this, but this does not work and applies the current class no matter what page you are on.

Code: Select all

<li><a <?php if ($page == 'carpetcleaning.php' or 'windowcleaning.php') { ?> class="current" <? php } ?> href="services.php">services</a></li>
Thanks,
Matt

Re: Selecting class

Posted: Fri Apr 29, 2011 12:35 am
by jaceinla
Use the html <title> of the page to solve your problems...

Services - Carpet Cleaning
Services - Window Cleaning

Then, use something like file_get_contents to grab the current page you're on (maybe a $_SERVER['PHP_SELF']) and store it as text, then preg_match the hell out of it using the <title> and </title> as your search points, then check whatever is in between it aka the actual title itself (Services - Carpet Cleaning) and extract just the category (use the - as a breaking point) and then save it as a variable like $current_page_category then jquery up the "current" class with

Code: Select all

$("<?php echo $current_page_category; ?>").addClass('current');
and you'll be good to go on every page even if you add new categories/pages

PS. I don't think you're using classnames like "services" for each menu item, so just add in some alt text so you can narrow it down to the exact li

Re: Selecting class

Posted: Fri Apr 29, 2011 2:09 am
by matthew.mcdougall
Hi jaceinla,

Thanks very much for that - very much appreciated. I'll give it a try and let you know.

Any other suggestions are still welcome!

Thanks,
Matt