How can one dynamically highlight a specific page number in a navigation menu using PHP?
To dynamically highlight a specific page number in a navigation menu using PHP, you can compare the current page number with the page number in the navigation menu and add a CSS class to the corresponding link to apply the highlighting effect. This can be achieved by checking the current page number against a variable or using a conditional statement to determine which link should be highlighted.
<?php
$current_page = 2; // Assuming the current page number is 2
for ($i = 1; $i <= 5; $i++) {
if ($i == $current_page) {
echo '<a href="#" class="highlighted">Page ' . $i . '</a>';
} else {
echo '<a href="#">Page ' . $i . '</a>';
}
}
?>
Keywords
Related Questions
- How can the issue of not redirecting resources from HTTP to HTTPS impact the functionality of a PHP script that checks URLs?
- How can clicks on a specific object be counted and stored on a website without using a database in PHP?
- What are common mistakes to avoid when using PHP to interact with a MySQL database for CRUD operations?