Mr_jmm Posted December 20, 2007 Share Posted December 20, 2007 Hi all, I'm trying to write a php script to replace that age old Javascript "uberlink". For those that don't know of "Uberlink" what I'm doing is using a php script that highlights a link to show you are on that page already. An example of what I'm using: if ($_SERVER[php_SELF] == '/aboutus.php') { $set_link = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"'; } else { $set_link = 'title="Find information about the website and it\'s owner"'; } Which adds relevant html into: $aboutus = '<a href="aboutus.php" target="_self" '.$set_link .'>About Us</a>'; and this works perfectly..... except when it comes to doing the same for a page that is the index page for a folder, eg if ($_SERVER[php_SELF] == '/portfolio/') { $set_linkp = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"'; } else { $set_linkp = 'title="Have a look at websites designed by ? ? ? ?"'; } The page still works correctly, as does the nav menu except no highlighted link, infact, none of the links show any title info. Is it possible to use this code for folders and / or could you please advise the correct method. Many thanks. James Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/ Share on other sites More sharing options...
roopurt18 Posted December 20, 2007 Share Posted December 20, 2007 Here is a snippet from a site I'm working on that does just that. Basically it sets up an array of navigation links, where each element in the array consists of the label, url, and a regular expression to check against the current URL. While displaying the navigation menu, I check the current URL with the regexp for the link; if it matches the regexp I set class="current" for the link, which has appropriate style info in the CSS. <div id="mainnav"> <?php $MainNav = Array(); $MainNav[] = Array( 'url' => '/', 'label' => 'Home', 'regexp' => '/^\/$/' ); $MainNav[] = Array( 'url' => '/software', 'label' => 'Software', 'regexp' => '/^((\/software)|(\/ibswin)|(\/webview)).*$/' ); $MainNav[] = Array( 'url' => '/support', 'label' => 'Support', 'regexp' => '/^\/support.*$/' ); $MainNav[] = Array( 'url' => '/pages/clients', 'label' => 'Clients', 'regexp' => '/^\/pages\/clients.*$/' ); $MainNav[] = Array( 'url' => '/pages/contact', 'label' => 'Contact', 'regexp' => '/^\/pages\/contact.*$/' ); $MainNav[] = Array( 'url' => '/pages/about', 'label' => 'About', 'regexp' => '/^\/pages\/about$/' ); echo '<ul>'; foreach($MainNav as $Nav){ if($Nav['regexp'] != '//' && preg_match($Nav['regexp'], $this->here)){ $class = ' class="current"'; }else{ $class = ''; } echo " <li> <a href=\"{$Nav['url']}\"{$class}>{$Nav['label']}</a> </li> "; } echo '</ul>'; ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419599 Share on other sites More sharing options...
Mr_jmm Posted December 20, 2007 Author Share Posted December 20, 2007 Thanks for that but is there a way to do this using a corrected version of my method? Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419604 Share on other sites More sharing options...
roopurt18 Posted December 20, 2007 Share Posted December 20, 2007 Can you provide an example of one of your menu items and the URLs it should match? Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419645 Share on other sites More sharing options...
Mr_jmm Posted December 20, 2007 Author Share Posted December 20, 2007 <ul class="navlist"> <li><?php echo $home; ?></li> <li><?php echo $aboutus; ?></li> <li><?php echo $portfolio; ?></li> <li><?php echo $faq; ?></li> <li><?php echo $contact; ?></li> </ul> $home = '<a href="./" target="_self" title="Return to the websites home page">Home</a>'; $aboutus = '<a href="aboutus.php" target="_self" '.$set_Astate .'>About Us</a>'; $portfolio = '<a href="./portfolio/" target="_self" '.$set_Pstate .'>Portfolio</a>'; $faq = '<a href="faq.php" target="_self" '.$set_Fstate .'>FAQ</a>'; $contact = '<a href="contact.php" target="_self" '.$set_Cstate .'>Contact Us</a>'; URLs: mycompany.net mycompany.net/aboutus.php mycompany.net/portfolio/ mycomapny.net/faq.php mycomapny.net/contact.php Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419657 Share on other sites More sharing options...
roopurt18 Posted December 20, 2007 Share Posted December 20, 2007 Based off everything you've said so far, the only one that is giving you trouble is the portfolio one. So how about this: <?php if (strpos($_SERVER['REQUEST_URI'], 'mycompany.net/portfolio') !== FALSE) { $set_linkp = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"'; } else { $set_linkp = 'title="Have a look at websites designed by ? ? ? ?"'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419761 Share on other sites More sharing options...
Mr_jmm Posted December 20, 2007 Author Share Posted December 20, 2007 Nope, sorry. (Yes, I changed "mycompany.net"). There are other links that refer to a folder but for simplicity just included the one but that's neither here or there. Is it just an improper use of $_SERVER or should it work? Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419782 Share on other sites More sharing options...
roopurt18 Posted December 20, 2007 Share Posted December 20, 2007 Go to one of the pages where this is not working and: echo $_SERVER['REQUEST_URI']; so that we can see the path as seen by the script. Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419796 Share on other sites More sharing options...
Mr_jmm Posted December 20, 2007 Author Share Posted December 20, 2007 That displays: /portfolio/ Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419799 Share on other sites More sharing options...
roopurt18 Posted December 20, 2007 Share Posted December 20, 2007 Try: <?php if (preg_match('/^\/portfolio.*/', $_SERVER['REQUEST_URI']) !== FALSE) { $set_linkp = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"'; } else { $set_linkp = 'title="Have a look at websites designed by ? ? ? ?"'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419821 Share on other sites More sharing options...
PHP_PhREEEk Posted December 20, 2007 Share Posted December 20, 2007 Would not: <?php if ($_SERVER[php_SELF] == '/portfolio/index.php') { $set_linkp = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"'; } else { $set_linkp = 'title="Have a look at websites designed by ? ? ? ?"'; } work...? PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419825 Share on other sites More sharing options...
PHP_PhREEEk Posted December 20, 2007 Share Posted December 20, 2007 Or possibly: <?php if ($_SERVER[sCRIPT_NAME] == '/portfolio/index.php') { $set_linkp = 'style="background:#fff;color:#00f;cursor:default;" title="You are here!"'; } else { $set_linkp = 'title="Have a look at websites designed by ? ? ? ?"'; } PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419829 Share on other sites More sharing options...
Mr_jmm Posted December 20, 2007 Author Share Posted December 20, 2007 No, definately no and no. Thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419836 Share on other sites More sharing options...
PHP_PhREEEk Posted December 20, 2007 Share Posted December 20, 2007 Well, what is loaded when those folders are called? An index.php, or .html? PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419847 Share on other sites More sharing options...
Mr_jmm Posted December 20, 2007 Author Share Posted December 20, 2007 It's index.html but I don't want to put that in the url (and I did change the code above to reflect that). (I'm in the middle of changing everything from .php to .html) Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419851 Share on other sites More sharing options...
Mr_jmm Posted December 20, 2007 Author Share Posted December 20, 2007 Curse that edit timer..... Strangely, strstr($_SERVER['PHP_SELF'], $_SERVER['REQUEST_URI'] returns /portfolio/index.html Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419907 Share on other sites More sharing options...
Mr_jmm Posted December 20, 2007 Author Share Posted December 20, 2007 er........ D'Oh! Remember folks, ALWAYS check file paths to include files!! I'm hanging my head in shame as you read this. Oh yes, nearly forgot, the original script works fine now. So, so sorry. Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419947 Share on other sites More sharing options...
Mr_jmm Posted December 21, 2007 Author Share Posted December 21, 2007 Phreeek: I apologise, adding index.html, although not necessary for my quoted example but is necessary for the home page, so, yes, good advice. Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-419993 Share on other sites More sharing options...
PHP_PhREEEk Posted December 21, 2007 Share Posted December 21, 2007 : nods : No need for any apology crap... we just want to get people where they want to go... heh glad you got 'er sorted, best of luck going forward! PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/82537-solved-if-_serverphp_self/#findComment-420010 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.