What are some potential issues with marking only top-level menu items as active in PHP navigation?
Potential issues with marking only top-level menu items as active in PHP navigation include the inability to highlight sub-menu items when their parent menu item is active. To solve this issue, you can modify the navigation logic to check if the current page URL matches any of the sub-menu items as well.
<?php
$current_page = basename($_SERVER['REQUEST_URI']);
$menu_items = array(
'Home' => 'index.php',
'About' => 'about.php',
'Services' => array(
'Web Design' => 'web-design.php',
'SEO' => 'seo.php'
),
'Contact' => 'contact.php'
);
foreach ($menu_items as $key => $value) {
if (is_array($value)) {
foreach ($value as $sub_key => $sub_value) {
if ($current_page == $sub_value) {
echo '<li class="active"><a href="' . $sub_value . '">' . $sub_key . '</a></li>';
} else {
echo '<li><a href="' . $sub_value . '">' . $sub_key . '</a></li>';
}
}
} else {
if ($current_page == $value) {
echo '<li class="active"><a href="' . $value . '">' . $key . '</a></li>';
} else {
echo '<li><a href="' . $value . '">' . $key . '</a></li>';
}
}
}
?>
Keywords
Related Questions
- Are there any recommended PHP classes or libraries for handling telnet connections in PHP?
- What are some best practices for excluding specific folders and files in a PHP script when creating a zip backup?
- In what scenarios would using placeholders for variables in template files be more advantageous than directly passing PHP-generated values?