Alex Sarnowski
Alex Sarnowski

Reputation: 739

PHP "if" inside of variable echo

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

Upvotes: 0

Views: 690

Answers (6)

PDR
PDR

Reputation: 71

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.
    */
    ?>

Upvotes: 0

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

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

Upvotes: 1

Jochem
Jochem

Reputation: 3387

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.

Upvotes: 0

Edd
Edd

Reputation: 683

I think you are looking for something like this:

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

Upvotes: 0

Ynhockey
Ynhockey

Reputation: 3932

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 ...';

Upvotes: 0

Dmitriy Sushko
Dmitriy Sushko

Reputation: 242

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

Upvotes: 1

Related Questions