wdallman Posted December 10, 2009 Share Posted December 10, 2009 I am using this code for a menu bar so that the "selected" id can be used by CSS for styling purposes. I can't figure out why this code won't work... <ul> <? $page = $_SERVER['SCRIPT_NAME']; ?> <? if ($page == '/index.php') { ?> <li><a href="/index.php" class="selected">Home</a></li> <li><a href="/contact.php">Contact Us</a></li> <li><a href="/agents.php">Meet our Agents</a></li> <li><a href="/claims.php">Submit a Claim</a></li> <? } else if ($page == '/contact.php') { ?> <li><a href="http://www.dallmaninsurance.com">Home</a></li> <li><a href="contact.php" class="selected">Contact Us</a></li> <li><a href="agents.php">Meet our Agents</a></li> <li><a href="claims.php">Submit a Claim</a></li> <? } else if ($page == '/agents.php') { ?> <li><a href="http://www.dallmaninsurance.com">Home</a></li> <li><a href="contact.php">Contact Us</a></li> <li><a href="agents.php" class="selected">Meet our Agents</a></li> <li><a href="claims.php">Submit a Claim</a></li> <? } else if ($page == '/claims.php') { ?> <li><a href="http://www.dallmaninsurance.com">Home</a></li> <li><a href="contact.php">Contact Us</a></li> <li><a href="agents.php">Meet our Agents</a></li> <li><a href="claims.php" class="selected">Submit a Claim</a></li> <? } else { ?> <li><a href="http://www.dallmaninsurance.com">Home</a></li> <li><a href="contact.php">Contact Us</a></li> <li><a href="agents.php">Meet our Agents</a></li> <li><a href="claims.php">Submit a Claim</a></li> <? } ?> </ul> On the web, it gets all the way to the last "else" and outputs those list items. In wampserver, it outputs all of the list items (so there are 5 menu bars). Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/184685-coding-help/ Share on other sites More sharing options...
premiso Posted December 10, 2009 Share Posted December 10, 2009 You may have to use stristr to check the file: <?php if (stristr($page, '/index.php')) { ?> <li><a href="/index.php" class="selected">Home</a></li> <li><a href="/contact.php">Contact Us</a></li> <li><a href="/agents.php">Meet our Agents</a></li> <li><a href="/claims.php">Submit a Claim</a></li> <?php } I would also convert your <? to <?php for compatibility reasons, as short_tags have been defaulted to off in php 4.x I believe. But see if that works for you. Quote Link to comment https://forums.phpfreaks.com/topic/184685-coding-help/#findComment-975049 Share on other sites More sharing options...
raytri Posted December 10, 2009 Share Posted December 10, 2009 A (not too) manual workaround would be to simply define $page at the top of each page. So on index.php, you'd write $page = '/index.php'; Also, what do you do if you have subdirectories with their own "index.php" files? Quote Link to comment https://forums.phpfreaks.com/topic/184685-coding-help/#findComment-975066 Share on other sites More sharing options...
wdallman Posted December 10, 2009 Author Share Posted December 10, 2009 Thank you! Premiso's suggestion worked. I'm using the code in an includes file, so I'm not sure that declaring $page on each site would work for me. I'm so new to this, though, that I could be completely wrong. Thank you so much for saving me a lot of time! How do I mark this problem as solved on this site (1st day on phpfreaks)? Quote Link to comment https://forums.phpfreaks.com/topic/184685-coding-help/#findComment-975078 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.