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; ?>
4
  • What's not working? What errors do you get that you don't understand? Commented Nov 25, 2012 at 9:29
  • What exactly is your question? What are you trying to do? Any errors? Commented Nov 25, 2012 at 9:31
  • You need to learn to how to create functions and how to refactor your code. If you just create a function that would iterate on an array that contains the names(tutorials for eg) and returns your navbar, then you could focus on your current problem not seeing all this duplicate code. Commented Nov 25, 2012 at 9:31
  • that code obviously isn't working, because you can't do <?php .. inside of a variable...so I was wondering how to do it (functioning like the way it looks [obviously using a different method] ) Commented Nov 25, 2012 at 9:32

6 Answers 6

1

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

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

Comments

1

There is nothing wrong with your variables inside the li tags, the problem is with the way you mix HTML and PHP code and tags inside single quotes. Nothing will work unless you correct that. Here is the way to do it correctly, using your own code:

<?php
if ( !isset( $_SESSION[ 'idx' ] ) ) { ///////////IF NOT LOGGED IN
  if ( !isset( $_COOKIE[ 'idCookie' ] ) ) { //////IF NOT LOGGED IN
    ?>

  <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>

  <?php }
  if ( isset( $_SESSION[ 'idx' ] ) ) { ////////////IF LOGGED IN (WITHOUT COOKIES)
  ?>

  <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 }  else {
    if ( isset( $_COOKIE[ 'idCookie' ] ) ) { //IF LOGGED IN (WITH COOKIES)
  ?>

    <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 
    }
  }
}
?>

Comments

0

While personally I would change the whole method of doing this (have an array that corresponds with your list and echo accordingly), with your method it can be done by separating the variable from the code, for example:

$array['tutorials'] = ($first_partmainnav == "tutorials") ? 'active' : 'noactive';
$array['resources'] = ($first_partmainnav == "resources") ? 'active' : 'noactive';
// etc...

Then concatenate it with your output.

In general you should use string concatenation:

Wrong:

echo '<a href="<?php echo $dyn_wwwFULL; ?>tutorials ...';

Right:

echo '<a href="'.$dyn_wwwFULL.'tutorials ...';

Comments

0

I think you are looking for something like this:

$first_partmainnav = 'tutorials';
echo 'some text ' . ($first_partmainnav == 'tutorials' ? 'active' : '') . ' some more text';

Comments

0

It is a while back that I coded in PHP but to be it looks like you are

How would the following piece of code work?

<?php

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

Trust you can fill in the rest yourself.

When I looked at syntax highlighting of the first line of < li> lines you notice that there is something odd happening.

Comments

0

The real problem is you cannot have PHP tags inside quoted PHP code. Let's check the last line of the first li block, in the question:

    <?php
    //NOTE: Had to add a space to separate php tags in comments for proper code highlighting, like this: < ? php --- ? >

    $navbar = '
            <li class="<?php if ($first_partmainnav=="community") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>community">Community</a></li>'; ?>

    <?php
    /* Although the whole $navbar value is quoted (Single quotes), there is php code surrounded by php tags in 2 places: 
    < ? php if ($first_partmainnav=="community") {echo "active"; } else  {echo "noactive";} ? > Here
    < ? php echo $dyn_wwwFULL; ? > And here
    Same happens with the rest of the lines, so it is impossible for this code to work.
    */
    ?>

The selected answer is also wrong. Let's see:

    <?php
      $navbar = '<li class="'. ($first_partmainnav=="" ? "active" : "noactive")  .'"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
      '; // Add the single quote an semicolon missing.

    /* Same problem: $navbar value is quoted but there is php code surrounded by php tags:
    <a href="< ? php echo $dyn_wwwFULL; ? > Here.                                       
    This code can't work either.
    */
    ?>

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.