Skip to content

Instantly share code, notes, and snippets.

@jimboroberts
Created August 5, 2014 09:12
Show Gist options
  • Save jimboroberts/5611a679af8e3ab217cf to your computer and use it in GitHub Desktop.
Save jimboroberts/5611a679af8e3ab217cf to your computer and use it in GitHub Desktop.
Creation navigation function array in bootstrap
function createNavigation(){
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
if(count($currentFile)>2){
if($currentFile[count($currentFile) - 1]=='index.php'){
$currentFile = '/'.$currentFile[count($currentFile) - 2].'/';
} else {
$currentFile = '/'.$currentFile[count($currentFile) - 2].'/'.$currentFile[count($currentFile) - 1];
}
} else {
if($currentFile[count($currentFile) - 1]=='index.php'){
$currentFile = '/';
} else {
$currentFile = '/'.$currentFile[count($currentFile) - 1];
}
}
// List of pages
$pages = array(
array("file" => "/", "title" => "Home"),
array("file" => "/services/", "title" => "Services", "children" => array(
array("file" => "/services/something", "title" => "Sub"),
)),
array("file" => "/about_OEE/", "title" => "About OEE"),
array("file" => "/contact/", "title" => "Contact OEE"),
);
$menuOutput = '<ul class="nav navbar-nav navbar-right">';
foreach ($pages as $page) {
$activeClassAppend = ($page['file'] == $currentFile) ? ' active' : "";
$subClassAppend = isset($page['children']) ? ' dropdown-toggle' : "";
$subDataAppend = isset($page['children']) ? ' data-toggle="dropdown"' : "";
$subSpanAppend = isset($page['children']) ? ' <span class="caret"></span>' : "";
if (isset($page['children'])){
$activeSubAppend = ($page['file'] == $currentFile) ? ' class="active"' : "";
$childen = '<ul class="dropdown-menu" role="menu">';
foreach ($page['children'] as $child) {
$childen .= '<li' . $activeSubAppend . '><a href="' . $child['file'] . '">'.$child['title'].'</a></li>';
}
$childen .= '</ul>';
}
$menuOutput .= '<li class="'.$activeClassAppend.' '.$subClassAppend.'"><a href="'.$page['file'].'" '.$subDataAppend.'>'.$page['title'].' '.$subSpanAppend.'</a>'
.@$childen.
'</li>';
}
$menuOutput .= '</ul>';
return $menuOutput;
}
<header class="header">
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<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="/"><img src="/images/logo.png" alt="logo" width="125" height="29" /></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<?php echo createNavigation(); ?>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</header>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment