Jump to content

Recommended Posts

Hi,

 

I have a website where my menu is put in each page with the include function. Inside the menu I have a few if statements to show the links on the navigation in different colors according to the page where I am.

 

To do that, I first define a variable, for instance $level1 = "Home". I then use the include to include the menu in the page.

 

Inside the menu.php file I have an if statement to show the Home link in a different color if I am in pages where $level1 == "Home", like this:

 

if ($level1 == "Home")
{
  //Code to change the color
}

 

However, it doesn't work and all links are set as if there were no if statements. I know the problem isn't in the "//code to change the color" because I have changed the condition to "true" and it worked.

 

Does php reset the variables inside the included file and if so how can I do this work? Or is it some other problem?

No it wont reset the variable in the include script, unless you have an iff like this if($var1='Home'), because a single = will set a value and not compare the two.

 

Can we see more code? So far it looks as if it would work

 

The code for the entire include, and the code for the initial variable declaration.

 

 

ILMV

In the index.php I have:

 

<?php $level1="Home"; include'menu.php'; ?>

 

In the menu.php:

 

<div id="primary">
  <ul>
    <li><a href="index.php" 
        <?php 
  if($level1 == "Home")
  { 
    echo'class="on"';
   }else{
     echo"rel=\"nofollow\"";
   } 
?>
    >Home</a></li>
    <li><a href="gold.php" <?php if($level1=="Gold"){ echo"class=\"on\"";}else{echo"rel=\"nofollow\"";} ?>>Gold</a></li>
    <li><a href="silver.php" <?php if($level1=="Silver"){echo"class=\"on\"";}else{echo"rel=\"nofollow\"";} ?>>Silver</a></li>
  </ul>
</div>

 

The code in the first if statement is not all in the same line like the others because I wanted to check that I hadn't some kind of syntax error.

Have you checked the output? For example, does it echo anything!? The reason I ask is because if it has echoed something, it just might not be working, and that might not be a PHP problem?

 

Looking at it, I cannot see anything wrong?!

 

 

Happy Xmas!

ILMV

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.