0

I'm currently building a dynamic Website using PHP

I'm having a nav menu like this:

<nav>
          <ul class="menu">
            <li class="current"><a href="?p=home">Startseite</a></li>
            <li><a href="?p=cars">Fahrzeuge</a></li>
            <li><a href="?p=anfahrt">Anfahrt</a></li>
            <li><a href="?p=impressum">Impressum</a></li>
          </ul>

Currently I'm indicating the clicked item by manually adding the css class "current" to that specific item. But, as my website is dynamic, since PHP is including files instead of having the pages manually, I need to indicate it programmatically, how do I go about that? Any Help is greatly appreciated My CSS:

header .highlight, header .current a{
  color:#e8491d;
  font-weight:bold;
}

header a:hover, header li.active{
  color:#cccccc;
  font-weight:bold;
3
  • 2
    Its simple. If ($_GET['p']=='cars'){ echo "current"; } into class="". You can try it. you write a function or switch or both Commented Nov 19, 2017 at 14:55
  • I tried that. But it doesn't work. It isn't indicating anything. Commented Nov 19, 2017 at 15:01
  • Nvm. My bad. Works perfectly now. thanks :P Commented Nov 19, 2017 at 15:04

1 Answer 1

1

Like the comment of @ivan-barayev:

<nav>
          <ul class="menu">
            <li class="<?php if( $_GET['p'] == 'home'){echo 'current';} ?>"><a href="?p=home">Startseite</a></li>
            <li class="<?php if( $_GET['p'] == 'cars'){echo 'current';} ?>"><a href="?p=cars">Fahrzeuge</a></li>
            <li class="<?php if( $_GET['p'] == 'anfahrt'){echo 'current';} ?>"><a href="?p=anfahrt">Anfahrt</a></li>
            <li class="<?php if( $_GET['p'] == 'impressum'){echo 'current';} ?>"><a href="?p=impressum">Impressum</a></li>
          </ul>

This should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.