boedromios Posted December 22, 2008 Share Posted December 22, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/138010-problem-with-include-variables-and-if-statements/ Share on other sites More sharing options...
ILMV Posted December 22, 2008 Share Posted December 22, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138010-problem-with-include-variables-and-if-statements/#findComment-721303 Share on other sites More sharing options...
boedromios Posted December 22, 2008 Author Share Posted December 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138010-problem-with-include-variables-and-if-statements/#findComment-721803 Share on other sites More sharing options...
ILMV Posted December 23, 2008 Share Posted December 23, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138010-problem-with-include-variables-and-if-statements/#findComment-722138 Share on other sites More sharing options...
boedromios Posted December 24, 2008 Author Share Posted December 24, 2008 It just started working miraculously (without me changing anything on the server itself!). I guess it's just an Xmas gift from Santa Thanks for the help, Merry Xmas Quote Link to comment https://forums.phpfreaks.com/topic/138010-problem-with-include-variables-and-if-statements/#findComment-723161 Share on other sites More sharing options...
Maq Posted December 24, 2008 Share Posted December 24, 2008 I guess it's just an Xmas gift from Santa Why do you get your presents before everyone else? Quote Link to comment https://forums.phpfreaks.com/topic/138010-problem-with-include-variables-and-if-statements/#findComment-723173 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.