In2Blues Posted June 19, 2017 Share Posted June 19, 2017 First, let me say that I am not a programmer and don't know PHP. I simply use it to load webpage sections that are common to multiple pages. I have followed numerous examples of PHP that will highlight a menu item when that is the current page but none of them seem to work. It worked fine before I switched to PHP and everything else with PHP works except this. Here is what my menu looked like pre-PHP: <ul id="nav" class="nav navbar-nav menu"> <li><a href="index.php" title="Go to Home Page"><span class="currentpage"> Home </span></a></li> <li><a href="about.php" title="Learn About Us">About</a></li> <li><a href="contact.php" title="Contact Us">Contact</a></li> </ul> Menu items are green text on white background. When the Home Page loaded, "Home" would change to white text on green background without any hovering, which is exactly what I wanted. The same for the other pages respectively. However, this highlighting doesn't work with PHP for some reason. I'm probably missing something so that's why I'm posting for help. Here are the various codes: In index.php: <!DOCTYPE html> <?php $current = 'home';?> <html lang="en"> <head> <?php include 'head.txt';?> </head> <?php include('main-nav.txt');?> The include for main-nav.txt is where my menu goes. In main-nav.txt: <nav class="collapse navbar-collapse navbar-right" role="navigation"> <ul id="nav" class="nav navbar-nav menu"> <li <?php if($current == 'home') {echo 'class="currentpage"';} ?>><a href="index.php" title="Go to Home Page"> Home </a></li> <li <?php if($current == 'aboutus') {echo 'class="currentpage"';} ?>><a href="about.php" title="Learn About Us"> About </a></li> <li <?php if($current == 'contact') {echo 'class="currentpage"';} ?>><a href="contact.php" title="Contact Us"> Contact </a></li> </ul> </nav> The classes you see in "nav" and "ul" are from the template I'm using. They are not mine. I tried moving the PHP from the <li> to the <a> but the results were the same. My CSS contains the font and background color changes: .currentpage { background-color:#00c800; color:#ffffff; } As far as I can tell this should work but, like I said, I'm not a programmer or experienced in PHP. Any help will be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 20, 2017 Share Posted June 20, 2017 The problem you're describing doesn't exist in the code you've shown, so something else is going on. Open the page in the browser and look at the HTML source code. What does it say? Is the class attribute set at all? Are you sure you're looking at the right PHP code? If you test this online (which I hope you don't), have you actually uploaded the latest version? Is it at the right location? Learn basic debugging steps like inspecting variables with var_dump(). Then you can actually check the value of $current. What's up with the ".txt" extension? This is for plaintext files. PHP scripts have a ".php" extension, as you already seem to know. The include statement will accept anything, but if you try to execute the file directly, it won't be executed -- which is a common newbie error. So fix that. Stop randomly switchting between PHP code and HTML markup. This, again, is a very common source for errors. Do all PHP processing on top of the script, then comes the HTML markup with only a few simple output-related PHP snippets. 1 Quote Link to comment Share on other sites More sharing options...
In2Blues Posted June 20, 2017 Author Share Posted June 20, 2017 The problem you're describing doesn't exist in the code you've shown, so something else is going on. Open the page in the browser and look at the HTML source code. What does it say? Is the class attribute set at all? Are you sure you're looking at the right PHP code? If you test this online (which I hope you don't), have you actually uploaded the latest version? Is it at the right location? Learn basic debugging steps like inspecting variables with var_dump(). Then you can actually check the value of $current. What's up with the ".txt" extension? This is for plaintext files. PHP scripts have a ".php" extension, as you already seem to know. The include statement will accept anything, but if you try to execute the file directly, it won't be executed -- which is a common newbie error. So fix that. Stop randomly switchting between PHP code and HTML markup. This, again, is a very common source for errors. Do all PHP processing on top of the script, then comes the HTML markup with only a few simple output-related PHP snippets. Hi Jacques1. Thanks for your reply. As I said, I'm not a programmer and don't know PHP so I'm unclear about some of your reply. Let's take it in order: I opened the page in the browser (should have done that before) and the class attribute is there but in the <li> as you can see below. <li class="currentpage"><a href="index.php" title="Go to Home Page"> Home </a></li> I moved it to a <span> as I had it pre-PHP and it works perfectly. Go ahead. You can say it. Stupid, stupid, stupid. I have to chalk it up to exhaustion and not seeing the obvious. I would like to learn more, so if I may continue with your reply. Now we know it's the right code and yes, I'm testing it online, but in a directory not on my main site. It's not technically "live". Learning debugging is probably a good idea. Thanks for the suggestion. I use a ".txt" file because I'm not using a PHP script per se, simply loading my HTML into each page. I thought that would be the way to go in this case. Randomly switchting between PHP code and HTML markup? I'm not sure exactly what you mean here. I'm only using PHP to load common page items in each page. Should I be doing something different here? Thanks for your help and your patience. I hope I haven't exasperated you. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 20, 2017 Share Posted June 20, 2017 Your observation doesn't really make sense. A green <li> element is a green <li> element, so it must definitely been highlighted before (unless you've somehow messed up the CSS). Or was it highlighted, just not in the way you wanted? Then you should have been more clear. Now we know it's the right code and yes, I'm testing it online, but in a directory not on my main site. It's not technically "live". If it's online, maybe even without password protection, then it is live. Don't do that. During development, there will be tons of serious bugs and exploitable security vulnerabilities, and you definitely don't want that anywhere near a real server. Set up a local test environment with something like XAMPP. It's very easy and saves you both a lot of time and a lot of pain. This is actually the very first thing you should do before continuing with the application. I use a ".txt" file because I'm not using a PHP script per se, simply loading my HTML into each page. It is a full-blown PHP script. Any file with executable PHP code is by definition a PHP script, no matter if contains 100% code or 1% code. And PHP scripts have a ".php" extension. Randomly switchting between PHP code and HTML markup? I'm not sure exactly what you mean here. I'm only using PHP to load common page items in each page. Should I be doing something different here? I'm saying that you should organize your scripts and separate the actual programming (setting variables etc.) from the visual output (the HTML markup). Right now, you start your HTML document with a doctype, then you suddenly switch to PHP mode to set a variable, then you go back to HTML mode. This kind of switching back and forth quickly leads to a total mess and actual bugs, because some code cannot be executed after you've already generated HTML output (like setting cookies). (Ab)using PHP includes to assemble HTML documents is also a rather poor approach, but since you're new to PHP, it may be an acceptable hack. Quote Link to comment Share on other sites More sharing options...
In2Blues Posted June 20, 2017 Author Share Posted June 20, 2017 Your observation doesn't really make sense. A green <li> element is a green <li> element, so it must definitely been highlighted before (unless you've somehow messed up the CSS). Or was it highlighted, just not in the way you wanted? Then you should have been more clear. If it's online, maybe even without password protection, then it is live. Don't do that. During development, there will be tons of serious bugs and exploitable security vulnerabilities, and you definitely don't want that anywhere near a real server. Set up a local test environment with something like XAMPP. It's very easy and saves you both a lot of time and a lot of pain. This is actually the very first thing you should do before continuing with the application. It is a full-blown PHP script. Any file with executable PHP code is by definition a PHP script, no matter if contains 100% code or 1% code. And PHP scripts have a ".php" extension. I'm saying that you should organize your scripts and separate the actual programming (setting variables etc.) from the visual output (the HTML markup). Right now, you start your HTML document with a doctype, then you suddenly switch to PHP mode to set a variable, then you go back to HTML mode. This kind of switching back and forth quickly leads to a total mess and actual bugs, because some code cannot be executed after you've already generated HTML output (like setting cookies). (Ab)using PHP includes to assemble HTML documents is also a rather poor approach, but since you're new to PHP, it may be an acceptable hack. You're right about the <li>. It highlighted when I hovered over it but not when the page loaded which is what I wanted. Sorry for not explaining that. I don't know anything about setting up a test environment like XAMPP. I know what a test environment is but don't know how to go about setting it up. I'm just looking to simplify my website setup. I will change the txt to php. Thanks for the explanation. I wouldn't know how to go about creating my website in PHP. I only know some HTML and using PHP includes. Maybe I should put everything in separate files and my index file would simply call all the elements into it? I really don't know. Thanks for your help. Quote Link to comment 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.