amelio Posted November 25, 2009 Share Posted November 25, 2009 Hi, I need a navigation include that can deal with directories at different levels on a site. All I can seem to find are the basic includes for files all in the same directory. This doesn't work for me because as soon as you navigate to a page in a nested directory the relative links don't work. Is the only option for me to use absolute links? This would cause difficulty as I would need different addresses to achieve the same result on the testing server and remote site. I also need to be able to identify the current page to style the link. At the moment I am using the code below which is fine whilst everything is in the same directory but introduce nested directories and some links will be reachable with just the $base. But only as long as current page and destination page are in the same directory. $base=basename($_SERVER['PHP_SELF']); $menu=preg_replace("|<li><a href=\"".$base."\">(.*)</a></li>|U", "<li class=\"current\">$1</li>", $menu); echo $menu; I would greatly appreciate your help. Link to comment https://forums.phpfreaks.com/topic/182946-navigation-include-for-all-site-directories/ Share on other sites More sharing options...
abazoskib Posted November 25, 2009 Share Posted November 25, 2009 Back to the basics. Directory A contains Directory B and a file name test.txt. You are in Directory B. So to access test.txt you would use ../test.txt Link to comment https://forums.phpfreaks.com/topic/182946-navigation-include-for-all-site-directories/#findComment-965641 Share on other sites More sharing options...
amelio Posted November 25, 2009 Author Share Posted November 25, 2009 Thanks abazoskib, Using your example. If I am in Directory B, ../test.txt allows me to get to test.txt in Directory A but if I am navigating to test.txt from within Directory A I would only need test.txt & not ../test.txt. This is my problem, how do I accomodate these multiple relative addresses within one include file? This example shows just two but in a larger site you might have multiple nested directories with many files named index.php. Link to comment https://forums.phpfreaks.com/topic/182946-navigation-include-for-all-site-directories/#findComment-965690 Share on other sites More sharing options...
abazoskib Posted November 25, 2009 Share Posted November 25, 2009 Well then your best bet would be to define a constant variable in a file called global_settings.php for example such as define("INDEX_PAGE","/path/to/file/index.php") Now, if you include that global file in all your pages using an absolute path to the global file, you can simply use INDEX_PAGE to include the index page. You can put all your constant definitions within that global file, so you will only have to edit that if your structure ever changes. Link to comment https://forums.phpfreaks.com/topic/182946-navigation-include-for-all-site-directories/#findComment-965705 Share on other sites More sharing options...
amelio Posted November 26, 2009 Author Share Posted November 26, 2009 I sat down for a good half hour trying to get my head around your solution abazoskib but to no avail. I'm sure this is a reflection on my lack of understanding rather than your explanation. I would need to see an example to get it. I have come up with a solution which allows me to include the same file in every page on the site but I just have to use an absolute path to that include file. So not fully satisfactory. I do the test because the site on the testing server is in a folder below the root. if ($_SERVER['SERVER_NAME'] == "localhost") { $testing = "/testing_folder"; } else { $testing = ""; }; echo "<ul id=\"main_nav\"> <li><a href=\"".$testing."/index.php\">Home</a></li> <li><a href=\"".$testing."/welcome.php\">Welcome</a></li> <li><a href=\"".$testing."/emails.php\">Emails</a></li> <li><a href=\"".$testing."/news/index.php\">Village News</a></li> </ul>"; Link to comment https://forums.phpfreaks.com/topic/182946-navigation-include-for-all-site-directories/#findComment-965996 Share on other sites More sharing options...
abazoskib Posted November 26, 2009 Share Posted November 26, 2009 global_settings.php <?php define("INDEX_PAGE","http://yoursite.com/absolute/path/to/file"); define("WELCOME_PAGE","http://yoursite.com/absolute/path/to/file"); define("EMAILS_PAGE","http://yoursite.com/absolute/path/to/file"); define("NEWS_INDEX_PAGE","http://yoursite.com/absolute/path/to/file"); ?> The above uses "http://yoursite.com/absolute/path/to/file", just replace it with the actual full path to each file. Now in any file where you need to include these as links, use like so: <?php require_once('global_settings.php'); //use the absolute server path to this file echo "<ul id=\"main_nav\"> <li><a href=\"".INDEX_PAGE."\">Home</a></li> <li><a href=\"".WELCOME_PAGE."\">Welcome</a></li> <li><a href=\"".EMAILS_PAGE."\">Emails</a></li> <li><a href=\"".NEWS_INDEX_PAGE."\">Village News</a></li> </ul>"; ?> The advantage to this is that all your links will be defined within one file, so if anything changes you can always update one single file, instead of having to update every single page. Link to comment https://forums.phpfreaks.com/topic/182946-navigation-include-for-all-site-directories/#findComment-966055 Share on other sites More sharing options...
amelio Posted November 27, 2009 Author Share Posted November 27, 2009 Thanks for that abazoskib, I will play around. I am hoping that by including the directory name in the constant as you have done, that will make it possible for me to identify the current page link so that I can style that. I will post back in a few days. Appreciate the time you spent to help me. Link to comment https://forums.phpfreaks.com/topic/182946-navigation-include-for-all-site-directories/#findComment-966610 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.