How can PHP be used to dynamically highlight the current page in a menu with over 100 subpages?
To dynamically highlight the current page in a menu with over 100 subpages, you can use PHP to check the current page URL against the menu item URLs and add a CSS class to the active menu item. This can be done by looping through the menu items and comparing the URLs.
<?php
$current_url = $_SERVER['REQUEST_URI'];
$menu_items = array(
'Home' => '/',
'About' => '/about',
'Services' => '/services',
// Add more menu items as needed
);
foreach ($menu_items as $title => $url) {
$class = ($current_url == $url) ? 'active' : '';
echo '<li class="' . $class . '"><a href="' . $url . '">' . $title . '</a></li>';
}
?>
Keywords
Related Questions
- Are there any PHP libraries or functions that can help with secure file uploads?
- In the context of the provided PHP code snippet, what improvements can be made to enhance the functionality and security of the file upload process?
- What are best practices for handling multi-dimensional arrays in PHP when working with JSON data?