1

I have the following code and am trying to figure out how to add an either or situation to a PHP command. For instance below I want to have more that one page name (ie: advertising.php) that will trigger the event adding the active class specified in the php at the top.

    <?php
function active($currect_page){
  $url_array =  explode('/', $_SERVER['REQUEST_URI']) ;
  $url = end($url_array);  
  if($currect_page == $url){
      echo 'active'; //class name in css 
  } 
}
?>

<div class="row"><!--BEGIN NAV ROW-->
<div class="container">
    <nav class="navbar navbar-default" role="navigation">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#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="index.php"><img src="images/site-logo.png" alt="Briley Design Group" /></a>
        </div>
        <div class="collapse navbar-collapse" id="collapse">
            <ul class="nav navbar-nav navbar-right list-hover-slide">
                <li class="<?php active('index.php');?>"><a href="index.php">HOME</a></li>

                <li class="<?php active('advertising.php');?> dropdown "><a href="#" class="dropdown-toggle" data-toggle="dropdown">PORTFOLIO</a>
                  <ul class="dropdown-menu">
                    <li><a href="advertising.php">Advertising</a></li>
                    <li><a href="annual.php">Annual Reports</a></li>
                    <li><a href="brochures.html">Brochures</a></li>
                    <li><a href="cards.html">Cards/Promo</a></li>
                    <li><a href="interactive.html">Interactive</a></li>
                    <li><a href="logos.html">Logos</a></li>
                    <li><a href="display.html">Display Graphics</a></li>
                    <li><a href="photography.html">Photography</a></li>
                   </ul>
                </li>

                <li class="<?php active('about.php');?> dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">ABOUT US</a>
                  <ul class="dropdown-menu">
                    <li><a href="about.php">About Our Company</a></li>
                    <li><a href="clients.html">What Our Clients Say</a></li>
                    </ul>
                </li>
                <li class="<?php active('how.php');?>"><a href="how.html">HOW WE WORK</a></li>
                <li class="<?php active('contact.php');?>"><a href="contact.html">CONTACT</a></li>
            </ul>
        </div>
    </nav>
</div>

  • You mean you are trying to add an `active` class to a bootstrap nav bar by detecting the page that you're on? If so, [here's an answer to do it in javsacript](http://stackoverflow.com/questions/11533542/twitter-bootstrap-add-active-class-to-li), which should be all that you need. – Ohgodwhy Dec 07 '15 at 19:29
  • yes, trying to add an active class. This is working except I want there to be more than one option (multiple page titles) that makes "portfolio" active because it has subnavigation that should make the main section active. – Pamela Funk Dec 07 '15 at 19:32

3 Answers3

2

An option (similar to ethans solution) would be to use the func_get_args method like this:

function active(){
  $url_array =  explode('/', $_SERVER['REQUEST_URI']);
  $url = end($url_array);  
  if(in_array($url, func_get_args())){
      echo 'active'; //class name in css 
  } 
}

Then you can call the method using:

<?php active("advertising.php", "something.php", "else.php"); ?>
Tobias Xy
  • 2,039
  • 18
  • 19
0

You can allow your active() function to accept an array of script names.

function active(array $current_page){
  $url_array =  explode('/', $_SERVER['REQUEST_URI']) ;
  $url = end($url_array);
  foreach ($current_page as $page) {}
      if($page == $url){
          echo 'active'; //class name in css 
          return;
      } 
  }
}

Then, for the portfolio menu, you would call it like this:

<?php active(array('advertising.php',"annual.php","brochures.html","cards.html", //etc)); ?>

ethan
  • 985
  • 6
  • 10
0

You could pass a regular expression to the function active, and test for a match. This can give you a lot of liberty in matching a url.

Also, although echo works fine, I would suggest to just return the class name, and then use <?= syntax to include the returned value in your html:

<?
function active($urlPattern) {
    return (preg_match('/\/(' . $urlPattern . ')\.\w+$/', $_SERVER['REQUEST_URI']))
        ? 'active' : ''; //class name in css 
}    
?>
...
<li class="<?= active("advertising|annual|brochures|cards|interactive|logos|display|photography") ?> dropdown">

So you use the pipe symbol to separate the different pages (without php or html extension) you want to have the active style for.

The function checks that one of these names appears between the last / and . of the current url.

With the power of regular expression you can do a lot more complex matches, you will maybe never need, but it's nice to have the power at hand.

trincot
  • 317,000
  • 35
  • 244
  • 286