Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3330796
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:31:26+00:00 2026-05-17T23:31:26+00:00

I have a dynamically generated variable that contains the currently selected city by the

  • 0

I have a dynamically generated variable that contains the currently selected city by the user. The format of the string is exactly the same as the href in the navigation menu below except without the prefix /city/.

Example:
    $user_city = 'london';
    $user_city = 'edinburgh';
...

I have the navigation menu below and I want to highlight the currently selected city, that is I want to add a class “active” to the currently selected city.

<ul>
    <li class="first"><a href="/city/all">All</a></li>
    <li><a href="/city/london">London</a></li>
    <li><a href="/city/liverpool">Liverpool</a></li>
    <li><a href="/city/edinburgh">Edinburgh</a></li>
    <li class="last"><a href="/city/glasgow">Glasgow</a></li>
</ul>

I know I could achieve this by putting each list item into an if statement looking whether $user_city equals the string in the href attribute. But I guess there must be a much smarter way?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:31:27+00:00Added an answer on May 17, 2026 at 11:31 pm

    If you have access to the menu generating function, you can do something like this:

    $cities   = array('London', 'Liverpool', 'Edingburgh', 'Glasgow');
    $userCity = 'London';
    echo '<ul><li class="first"><a href="/city/all">All</a></li>', PHP_EOL;
    foreach($cities as $city) {
        printf(
            '<li><a href="/city/%s"%s>%s</a></li>%s',
            strtolower(htmlentities($city)),
            $userCity === $city ? ' class="active' : '', // conditional
            (htmlentities($city)),
            PHP_EOL
        );
    }
    echo '</ul>';
    

    In other words, add a conditional that checks whether the current city stored in $userCity is the one currently printed on screen and if so add a class to it.

    Give me a comment if you update your question with your menu generating function. I will show you how to modify it with a conditional then (if you cant figure it from the above now).


    If you have to modify the menu after it’s generated and you can be sure the structure is always the same, then you can simply str_replace

    $menu = 'a string containing your menu HTML';    
    $city = 'london';
    echo str_replace(
        sprintf('<li><a href="/city/%s">', $city),
        sprintf('<li><a href="/city/%s" class="active">', $city),
        $menu
    );
    

    If you need more difficult string replacements, consider using an HTML parser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.