tags:

views:

241

answers:

5
+2  A: 

I think you need to put your $page = 'one'; above the require_once.. otherwise I don't understand the question.

CharlesLeaf
Hi!There is no difference if I put $page before or after require_once.It still prints the same thing:<div id="nav"> <ul> <li><a <? if($page == 'one'): ?> class="active"<? endif ?> href="index.php">Tab1</a>/</li> <li><a href="two.php">Tab2</a></li> <li><a href="three.php">Tab3</a></li> </ul> </div>
meow
PHP gets not parsed in heredoc strings.
Felix Kling
Ah wait I overlooked something. You have your entire code inside an `<<<EOD` block. The PHP code you have inside there won't get parsed. Try removing the first 2 and the last 2 lines in your common.php file. (so you only have the HTML left).
CharlesLeaf
A: 

Why don't you create a function or class for this navigation and put there active page as a parameter? This way you'd call it as, for example:

$navigation = new Navigation( 1 );

or

$navigation = navigation( 1 );
Ondrej Slinták
+4  A: 

why don't you do it like this:

in the pages:

<html>
   <head></head>
   <body>
      <?php $page = 'one'; include('navigation.php'); ?>
   </body>
</html>

in the navigation.php

<div id="nav">
   <ul>
      <li>
          <a <?php echo ($page == 'one') ? "class='active'" : ""; ?> 
                 href="index1.php">Tab1</a>/</li>
      <li>
          <a <?php echo ($page == 'two') ? "class='active'" : ""; ?> 
                  href="index2.php">Tab2</a>/</li>
      <li>
          <a <?php echo ($page == 'three') ? "class='active'" : ""; ?> 
                  href="index3.php">Tab3</a>/</li>
   </ul>
</div>

You will actually be able to control where in the page you are putting the navigation and what parameters you are passing to it.

Later edit: fixed syntax error.

Toader Mihai Claudiu
Comparing your code and his (which while there is an issue with his use of the heredoc syntax works just fine), I would prefer to come into code looking at his. Variables are declared at the top of the page, and the variables are used to place the navigation. Simple formatting issue, but personal preference. In any case, while your answer is good, it does not address the question asked.
Joseph
i changed it to <?php echo ($page == 'one') ? "class='active'" : ""; ?> and it totally worked! THANKS you rock!
meow
There is a principle that i come to cherish since i started to do programming. It's called the locality of information. Basically you would prefer to have all the information required to reason about some code near itself. this allow you to be able to decide locally about the purpose of some piece of code and force you to go in n places to gather that information.
Toader Mihai Claudiu
@Joseph: Normally i would have written the following: <?php $page = 'one'; include('navigation.php'); ?> something like this: <?php echo navigation(array('page' => 'one')); ?> but it would have been more involved to exemplify easily.
Toader Mihai Claudiu
so, is it to prefer to make a whole new nav.php file, or would you rather do like joseph suggested, simply use ".(($page == 'one') ? 'class="active"' : '')." in the string ?
meow
@meow: According to your original post, you already had a separate nav file called common.php. Whether you use Toader's recommendation on how to format the code or mine makes no difference. You will end up with the same result. It is all personal preference really.
Joseph
@Toader: I understand the difficulty in explaining there. I was just trying to point out that there really was no difference in the functionality of the two files, but that the formatting was different. Again, it was simply personal preference.
Joseph
okay thanks for that. i've chosen your soloution joseph since i've got more stuff in my common.php file, meaning i need the <?php - tags. thanks to both of you
meow
+1  A: 
  1. $page='one' should occur before you require_once() not after. After is too late- the code has already been required, and $nav has already been defined.

  2. You should use include('header.php'); and include('footer.php'); instead of setting a $nav variable early on. That increases flexibility.

  3. Make more functions. Something like this really makes things easier to follow:

    function maybe($x,$y){return $x?$y:'';}
    function aclass($k){return " class=\"$k\" "; }
    

    then you can write your "condition" like this:

    <a href="..." <?= maybe($page=='one',aclass('active')) ?>> ....
    
geocar
+1  A: 

Your index.php code is correct. I am including the updated code for common.php below then I will explain the differences.

<?php 
     $class = ($page == 'one') ? 'class="active"' : '';
     $nav = <<<EOD
        <div id="nav">
            <ul>
               <li><a $class href="index.php">Tab1</a>/</li>
               <li><a href="two.php">Tab2</a></li>
               <li><a href="three.php">Tab3</a></li>
           </ul>
        </div>
 EOD;
 ?>

The first issue is that you need to make sure that the end declaration for your heredoc -- EOD; -- is not indented at all. If it is indented, then you will get errors.

As for your issue with the PHP code not running within the heredoc statement, that is because you are looking at it wrong. Using a heredoc statement is not the same as closing the PHP tags. As such, you do not need to try reopening them. That will do nothing for you. The way the heredoc syntax works is that everything between the opening and closing is displayed exactly as written with the exception of variables. Those are replaced with the associated value. I removed your logic from the heredoc and used a tertiary function to determine the class to make this easier to see (though I don't believe any logical statements will work within the heredoc anyway)

To understand the heredoc syntax, it is the same as including it within double quotes ("), but without the need for escaping. So your code could also be written like this:

<?php 
     $class = ($page == 'one') ? 'class="active"' : '';
     $nav = "<div id=\"nav\">
            <ul>
               <li><a $class href=\"index.php\">Tab1</a>/</li>
               <li><a href=\"two.php\">Tab2</a></li>
               <li><a href=\"three.php\">Tab3</a></li>
           </ul>
        </div>";
 ?>

It will do exactly the same thing, just is written somewhat differently. Another difference between heredoc and the string is that you can escape out of the string in the middle where you can't in the heredoc. Using this logic, you can produce the following code:

<?php 
     $nav = "<div id=\"nav\">
            <ul>
               <li><a ".(($page == 'one') ? 'class="active"' : '')." href=\"index.php\">Tab1</a>/</li>
               <li><a href=\"two.php\">Tab2</a></li>
               <li><a href=\"three.php\">Tab3</a></li>
           </ul>
        </div>";
 ?>

Then you can include the logic directly in the string like you originally intended.

Whichever method you choose makes very little (if any) difference in the performance of the script. It mostly boils down to preference. Either way, you need to make sure you understand how each works.

Joseph
thanks very much for that! everything makes sense to me!
meow
so, what is best, make a new file nav.php like Toader Mihai Claudiu suggested, or simply use ".(($page == 'one') ? 'class="active"' : '')." in the string ? since both ways work..
meow