php show active page with code examples

PHP is a popular server-side scripting language that is widely used to create dynamic websites and web applications. One of the common tasks in web development is to indicate the active page on a website, which can be accomplished using several different methods.

One approach is to use the $_SERVER['PHP_SELF'] variable, which returns the name of the currently executing script. By comparing this value to the URLs of the navigation links on the website, the active link can be identified and given a specific CSS class. For example:

<nav>
  <a href="index.php" <?php if ($_SERVER['PHP_SELF'] == '/index.php') echo 'class="active"'; ?>>Home</a>
  <a href="about.php" <?php if ($_SERVER['PHP_SELF'] == '/about.php') echo 'class="active"'; ?>>About</a>
  <a href="contact.php" <?php if ($_SERVER['PHP_SELF'] == '/contact.php') echo 'class="active"'; ?>>Contact</a>
</nav>

Another approach is to use the $_SERVER['REQUEST_URI'] variable, which returns the requested URI, including the query string. By comparing this value to the URLs of the navigation links, the active link can be identified and given a specific CSS class.

<nav>
  <a href="index.php" <?php if ($_SERVER['REQUEST_URI'] == '/index.php') echo 'class="active"'; ?>>Home</a>
  <a href="about.php" <?php if ($_SERVER['REQUEST_URI'] == '/about.php') echo 'class="active"'; ?>>About</a>
  <a href="contact.php" <?php if ($_SERVER['REQUEST_URI'] == '/contact.php') echo 'class="active"'; ?>>Contact</a>
</nav>

Another approach is to use a function to check the current page and add the active class accordingly.

function set_active($path, $active = 'active') {
    return Request::is($path) ? $active : '';
}
<nav>
  <a href="{{ url('/') }}" class="{{ set_active('/') }}">Home</a>
  <a href="{{ url('about') }}" class="{{ set_active('about') }}">About</a>
  <a href="{{ url('contact') }}" class="{{ set_active('contact') }}">Contact</a>
</nav>

There are other different approaches to highlight the active pages, like using JavaScript or using a PHP framework's built-in functions. But these examples should give you a good starting point to implement active page highlighting on your website using PHP.

It is important to note that in order to use the above code snippets, you will need to have a basic understanding of HTML and CSS, as well as some knowledge of how PHP works. Additionally, make sure to test your code on multiple browsers to ensure that it works as expected.

Another related topic that is often used in conjunction with active page highlighting is breadcrumb navigation. Breadcrumb navigation is a type of navigation that shows the user their current location within the website's hierarchy, and allows them to quickly navigate to higher-level pages. This can be implemented in PHP by parsing the URL and extracting the relevant parts to create the breadcrumb links.

For example, if the current URL is www.example.com/products/books/novels, the breadcrumb navigation would be:

<a href="www.example.com">Home</a> > <a href="www.example.com/products">Products</a> > <a href="www.example.com/products/books">Books</a> > Novels

Another related topic is creating dynamic menus in PHP. This can be accomplished by storing the menu items in a database and using PHP to retrieve and display the items on the website. This allows for easy updating and management of the menu, as well as the ability to create different menus for different user roles or pages.

For example, by using a database, you can create a table that stores all the menu items, and use PHP to retrieve the items and build the menu dynamically.

$query = "SELECT * FROM menu_items";
$result = mysqli_query($conn, $query);

echo '<ul>';
while ($row = mysqli_fetch_array($result)) {
    echo '<li><a href="'.$row['link'].'">'.$row['title'].'</a></li>';
}
echo '</ul>';

It is also possible to create dynamic submenus in a similar way. For example, you could have a main menu with categories, and then when a user clicks on one of the categories, a submenu with the corresponding subcategories will appear.

In conclusion, highlighting the active page, creating breadcrumb navigation, and creating dynamic menus are all important aspects of web development using PHP. By using the techniques discussed in this article, you can create a more user-friendly and navigable website for your users.

Popular questions

  1. What is the purpose of highlighting the active page on a website?
    The purpose of highlighting the active page on a website is to give the user a clear indication of their current location within the website's hierarchy. This can improve user experience and navigation.

  2. What is the $_SERVER['PHP_SELF'] variable in PHP?
    The $_SERVER['PHP_SELF'] variable in PHP is a superglobal that returns the name of the currently executing script. This can be used to determine the active page and highlight it on the website.

  3. How can you use the $_SERVER['REQUEST_URI'] variable to identify the active page?
    The $_SERVER['REQUEST_URI'] variable in PHP returns the requested URI, including the query string. By comparing this value to the URLs of the navigation links, the active link can be identified and given a specific CSS class.

  4. What is the purpose of breadcrumb navigation?
    Breadcrumb navigation is a type of navigation that shows the user their current location within the website's hierarchy, and allows them to quickly navigate to higher-level pages. This can improve user experience and navigation.

  5. How can you create dynamic menus in PHP?
    Dynamic menus in PHP can be created by storing the menu items in a database and using PHP to retrieve and display the items on the website. This allows for easy updating and management of the menu, as well as the ability to create different menus for different user roles or pages.

Tag

Navigation.

Posts created 2498

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top