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 8881707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:23:55+00:00 2026-06-14T20:23:55+00:00

I am creating a navigation bar that automatically changes the class to active if

  • 0

I am creating a navigation bar that automatically changes the class to “active” if the page is currently active (using php if statements [using the current URL to match])

I also want to be able to change the header depending on if a user is logged in or not…now i usually would have no issue with this, however, because there are if statements inside of the variable, I do not know how to proceed.

My Problem is, it’s impossible to do if statements inside of variable estabilsihing…for example this is what I’m attempting to do, however it’s not working…is there a way of doing this, and actually making it work…
thank you in advanced!

MY CODE

—THE PHP—

In the head:

<?php
///// (GETS THE PARTS OF THE CURRENT URL)
error_reporting(0);
$directoryURIbody = $_SERVER['REQUEST_URI'];
$pathbody = parse_url($directoryURIbody, PHP_URL_PATH);
$componentsbody = explode('/', $pathbody);
$first_partsbody = $componentsbody[1];
$second_partsbody = $componentsbody[2];
$third_partsbody = $componentsbody[3];
$fourth_partsbody = $componentsbody[4];
$fifth_partsbody = $componentsbody[5];
?>

In Body:

:

<?php

if (!isset($_SESSION['idx'])) { ///////////IF NOT LOGGED IN
  if (!isset($_COOKIE['idCookie'])) {//////IF NOT LOGGED IN
      $navbar = '
        <li class="<?php if ($first_partmainnav=="") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
        <li class="<?php if ($first_partmainnav=="tutorials") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>tutorials">Tutorials</a></li>
        <li class="<?php if ($first_partmainnav=="resources") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>resources">Resources</a></li>
        <li class="<?php if ($first_partmainnav=="library") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>library">Library</a></li>
        <li class="<?php if ($first_partmainnav=="our-projects") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>our-projects">Our Projects</a></li>
        <li class="<?php if ($first_partmainnav=="community") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>community">Community</a></li>';
}
if (isset($_SESSION['idx'])) { ////////////IF LOGGED IN (WITHOUT COOKIES)

      $navbar = '
        <li class="<?php if ($first_partmainnav=="") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
        <li class="<?php if ($first_partmainnav=="whatever") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>whatever">whatever</a></li>
        <li class="<?php if ($first_partmainnav=="justanother") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>justanother">Just Another</a></li>
        ';

} else if (isset($_COOKIE['idCookie'])) {//IF LOGGED IN (WITH COOKIES)

      $navbar = '
        <li class="<?php if ($first_partmainnav=="") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
        <li class="<?php if ($first_partmainnav=="whatever") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>whatever">whatever</a></li>
        <li class="<?php if ($first_partmainnav=="justanother") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>justanother">Just Another</a></li>
        ';    

}
?>

<?php echo $navbar; ?>
  • 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-06-14T20:23:56+00:00Added an answer on June 14, 2026 at 8:23 pm

    You can use ternary operator and use string concatenation instead of echoing:

    <?php
    
    if (!isset($_SESSION['idx'])) { ///////////IF NOT LOGGED IN
      if (!isset($_COOKIE['idCookie'])) {//////IF NOT LOGGED IN
      $navbar = '
        <li class="'. ($first_partmainnav=="" ? "active" : "noactive")  .'"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
    
    ?>
    

    And the rest by analogy

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

Sidebar

Related Questions

I'm currently trying to create a custom navigation-bar for my navigation-controller in iOS using
First off I'm new at programming. I'm creating an app with one navigation bar
I am trying to create a navigation bar that will sit at the top
I had this idea for a website of creating a fixed horizontal navigation bar
I've run into interesting problem while creating navigation bar with two dynamic size columns
I am creating an app in which I am using navigation Controller. I had
Dreamweaver has spry menu function which lets user creating navigation bar quickly. Suppose I
When I am putting two right bar buttons in my navigation bar at that
I'm creating a horizontal ul navigation bar. Each li item is a different width.
I'm creating a simple Navigation-based app using core data for my iPhone and I'm

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.