In order to integrate icons into the navbar I use key=>value arrays to store a file name with an icon tag. In the navbar I use a foreach loop to build dropdown menus or index into an array for an individual navlink. This also allows to dynamically build and alter the dropdown menus very easily.
$homepage = array(
'index.php'=>'<i class="fa fa-home"></i>'
);
$guestpages = array(
'createaccount.php'=>'<i class="fa fa-university"></i>',
'login.php'=>'<i class="fa fa-sign-in"></i>'
);
$logout = array(
'logout.php'=>'<i class="fa fa-sign-out"></i>'
);
$pages = array($homepage,$guestpages,$logout);
I also parse the URL to determine what page the client is viewing.
$pagename = basename($_SERVER['PHP_SELF']);
And in order to associate the parsed URL with the appropriate icon tag from the $pages array, I currently use a nested foreach loop:
foreach ($pages as $pagearray) {
foreach ($pagearray as $page => $icon) {
if($pagename == $page) {
$pageicon = $icon;
}
}
}
And what I'd like to do instead is something like this:
$pageicon = $pages[?][$pagename];
Does a similar alternative solution exist?
$pages = ['index.php' => ...], you could simply do$pages[basename($_SERVER['PHP_SELF'])]…