4

I tried to make an Dynamic navbar for my website with php, and it worked This is how it looks like nowalt text...

And this is how I want to let it look like the page were I'm on is selected

alt text..

My code of the navbar:

<?php
echo "<nav class=\"navbar navbar-inverse\" role=\"navigation\">
      <div class=\"container\">
        <div class=\"navbar-header\">
          <a href=\"#\" class=\"navbar-brand\">
            Techzone
        </div>
        <div class=\"collapse navbar-collapse\">
        <ul class=\"nav navbar-nav\">
        <li><a href=\"/index.php\">Home</a></li>
        <li class=\"dropdown\"> <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">What we offer<b class=\"caret\"></b></a>
        <ul class=\"dropdown-menu\">
        <li><a href=\"/offers/websites.php\" class=\"glyphicon glyphicon-book\"> Websites</a></li>
        <li><a href=\"/offers/translations.php\" class=\"glyphicon glyphicon-font\"> Translations</a></li>
        <li><a href=\"/offers/programs.php\" class=\"glyphicon glyphicon-pencil\"> Programs</a></li>
        </ul>
        </li>
        <li><a href=\"/websitesMade.php\">Our websites <span class=\"badge\">2</span></a></li>
        <li><a href=\"/aboutus.php\">About Us</a></li>
        </ul>
        <form class=\"navbar-form navbar-right\" role=\"search\">
        <div class=\"form-group\">
        <input type=\"text\" class=\"form-control\" placeholder=\"Search\">
        </div>
        <button type=\"submit\" class=\"btn btn-default\">Search</button>
        </form>
        </div>
      </div>
    </nav>";
        }
        echo "</ul>
        <ul class=\"nav navbar-nav navbar-right\">
        <li class=\"dropdown\">
          <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">Language <span class=\"caret\"></span></a>
          <ul class=\"dropdown-menu\" role=\"menu\">
            <li><a href=\"../nl/index.php\">Nederlands</a></li>
            <li class=\"divider\"></li>
            <li class=\"active\"><a href=\"#\">English</a></li>
          </ul>
        </li>
      </ul>

        <form class=\"navbar-form navbar-right\" role=\"search\">
        <div class=\"form-group\">
        <input type=\"text\" class=\"form-control\" placeholder=\"Search\">
        </div>
        <button type=\"submit\" class=\"btn btn-default\">Search</button>
        </form>
        </div>
      </div>
    </nav>
";
?>

In my HTML page I'm using this code to let the navBar show up: <?php include '../navbar.php' ?>

BTW, I'm using Twitter-Bootstrap for the navbar

Thanks in advance,

Community
  • 1
  • 1
bramhaag
  • 135
  • 1
  • 1
  • 18

1 Answers1

0

Certainly not the nicest solution but you can get the name of the webpage:

$bn = basename($_SERVER['PHP_SELF']); /* Returns The Current PHP File Name */

then run a check whenever you go to display it (or ofc a nicer method)

if($bn == "index.php") {
    $str += '<li><a class="current" href=\"/index.php\">Home</a></li>'
} else {
    $str += '<li><a href=\"/index.php\">Home</a></li>'
}
user3047190
  • 319
  • 2
  • 7
  • Do I have to put the most upper code in the navbar.php class? I did think about this method, but I though that it would just return navbar.php, but I will try this in a few minutes – bramhaag Aug 15 '14 at 16:55
  • Hmm, yeah didn't think of that, here's an SO question on that: http://stackoverflow.com/questions/11905140/php-pass-variable-to-include – user3047190 Aug 15 '14 at 16:58
  • I don't know what that is about, but I guess I can just for example make a form that gets triggered when someone loads the page, and that sends the name of the page to the php class – bramhaag Aug 15 '14 at 17:00
  • It seems like I can't manage it to create a efficient piece of code that isn't 100 lines long to solve this... – bramhaag Aug 15 '14 at 18:07