0

Setting "active" status on a drop down menu when the nav.php file is included in.

This post is not as much a question as it is a php novice level solution to a common problem.

I constructed my website using the common method of:

<?php      
include "admin.php"; // handles MySQL connection and mysqli prepared stmts. etc. 
include "header.php";  
include "nav.php";  // nav with drop down menu        
?>
<!-- html of the page -->

Everything was fine until I tried to set "active" the appropriate tab and sub-tab. Surely any website designer has run into this issue. I tried googling for answers to no real satisfaction. So I came up with my own.

Let's say a website has 4 tabs (about_us products faq contact) and the deepest drop down is 6 sub-tabs.

I added this line to the bottom of admin.php:

$about = $prod = $faq = $cont = $sub1 = $sub2 = $sub3 = $sub4 = $sub5 = $sub6 = "nothing";

Wasn't sure if I needed to but I add this to the CSS

.nothing { }

The top of each page of the website is:

<?php          
include "admin.php"; 

/** if this page corresponds to the products tab and the fifth sub menu **/ 
$prod = $sub5 = "active"; // or whatever css uses as the class name 

include "header.php";  
include "nav.php";  // nav with drop down menu            
?>

In nav.php the css class name is echoed in

<div id="centeredmenu">
 <nav id="nav">
  <ul>
    <li class="<?php echo $about; ?>"><a href="about.php">About Us</a>
       </li>    
    <li class="<?php echo $prod; ?>"><a href="/products/index.php">Our Products</a>
      <ul>
        <li class="<?php echo $sub1; ?>"><a href="/products/index.php">Wedgets</a></li>
        <li class="<?php echo $sub2; ?>"><a href="/products/grommets.php">Grommets</a></li>
        <li class="<?php echo $sub3; ?>"><a href="/products/some.php">Some</a></li>
        <li class="<?php echo $sub4; ?>"><a href="/products/more.php">More</a></li>
        <li class="<?php echo $sub5; ?>"><a href="/products/useless.php">Useless</a></li>
        <li class="<?php echo $sub6; ?>"><a href="/products/stuff.php">Stuff</a></li>
     </ul>
   </li>

So there's my novice method of setting active an included nav file.

I would love to see how the pro's would solve this problem. Let's give the checkmark to whichever solution gets the most up votes by their peers. I am sure it won't be mine.

Tom
  • 55
  • 2
  • 9
  • Have a look at this Q&A's http://stackoverflow.com/q/17467614/ there might be something in there you could find useful. – Funk Forty Niner Sep 29 '14 at 00:48
  • I knew $_SERVER['PHP_SELF'] would have been an optional method. What I don't know how to do is use $_SERVER to get the folder name. ie /products/ I'm sure a little time spent googling would offer the how. – Tom Sep 29 '14 at 01:45
  • Have a look through the manual http://php.net/manual/en/reserved.variables.server.php - there's a whole bunch of goodies in there ;) – Funk Forty Niner Sep 29 '14 at 01:46
  • Check out also http://stackoverflow.com/a/3429268/ and http://php.net/manual/en/function.dirname.php – Funk Forty Niner Sep 29 '14 at 01:50
  • 1
    Don't normally write one line code but this worked: echo implode(array_slice(explode("/", dirname($_SERVER['PHP_SELF'])), -1, 1)); – Tom Sep 29 '14 at 03:25
  • Right on Tom, glad to hear it, *cheers* - thanks for the update. – Funk Forty Niner Sep 29 '14 at 03:26
  • You know Tom, that you can put it in as a self-answer, then accept it when Stack lets you. This could prove informative for others visiting the question. – Funk Forty Niner Sep 29 '14 at 03:31

2 Answers2

0

wouldnt it be easier to handle all that with CSS, hiding the stylings, and make the styling in the current page active (unhide it), that way the classes will always be the same but wont show, less if statements, less assignments I guess easier to maintain.

luisluix
  • 547
  • 5
  • 17
0

If I correctly followed the breadcrumbs that Fred -ii- was dropping, I will offer another method of setting "actve".

http://example.com/someone/has/too/complicated/of_a/dirctory/structure.php

In my case since I already have an admin file the php code would go in admin.php

admin.php

<?php
  $path = $_SERVER['PHP_SELF'];

  // get filename
  $file = $_SERVER[PHP_SELF]; 
  $file = basename($file);  //structure.php

  // get directory
  $dir = dirname($path); 
  $dir = explode("/", $dir);
  $dir = array_slice($dir, -1, 1); // by adjusting the offset each directory name can be isolated 
  $dir = implode($dir);
  ?>

and in nav.php

<div id="centeredmenu">
 <nav id="nav">
  <ul>
    <li <?php if($file === "about.php")echo 'class="active"'; ?>><a href="about.php">About Us</a>
       </li>    
    <li <?php if($dir === "products")echo 'class="active"'?>><a href="/products/index.php">Our Products</a>
      <ul>
        <li <?php if ($file === "index.php")echo 'class="active"'; ?>><a href="/products/index.php">Wedgets</a></li>
      </ul>
   </li>

The only problem I see with getting the directory name in this method is that you are working backwards (from /structure.php back to /someone/

This would be especially confusing when working with a deep directory structure where it is inconsistent which directory level is supposed to be the one set to "active".

So for the purpose of drop down menus, I personally would use:

admin.php

  <?php
  // get directory
  $dir = dirname($path); 
  $dir = explode("/", $dir);
  $dir = array_reverse ($dir, true); // Reverse the order 

  // and then select the directory names by referring to their depth in the directory structure.

  $dir1 = isset($dir[1])? $dir[1]) : NULL; //someone
  $dir2 = isset($dir[2])? $dir[2]) : NULL; //has 
  $dir3 = isset($dir[3])? $dir[3]) : NULL; //too
  $dir4 = isset($dir[4])? $dir[4]) : NULL; //complicated
  $dir5 = isset($dir[5])? $dir[5]) : NULL; //of_a
  $dir6 = isset($dir[6])? $dir[6]) : NULL; //directory
  ?>

nav.php

<li <?php if ($dir4 === "complicated")echo 'class="active"'; ?>>
<a href="/someone/has/too/complicated/index.php">Wedgets</a></li>

// another area of the website could be:
<li <?php if ($dir4 === "this")echo 'class="active"'; ?>>
<a href="/someone/keeps/making/this/directory/structure/too/long/index.php">More_Wedgets</a></li>
Tom
  • 55
  • 2
  • 9