Zephni Posted April 12, 2011 Share Posted April 12, 2011 Hi, this does work, but I want to check if it's the best way to do it. you can see it here at http://www.zephni.com/test/breadcrumb/. If you don't know, breadcrumbs are what sites use so you can easily navigate back the way you came like: PHP Freaks Forums » PHP Coding » PHP Coding Help » Like how you see on your page. Anywho, this is the PHP script here: <?php #get server name $domain = "http://".$_SERVER['SERVER_NAME']; #Gets URL and explodes into array $crumbs = explode("/",$_SERVER["REQUEST_URI"]); #echos out the breadcrumbs echo "<a href='$domain'>Home</a>"; #always first in line foreach($crumbs as $crumb){ if($crumb > ""){ $tree = null; #reset tree so it can build up again for each foreach (*confuse face*) echo " > "; #all this part below does is capitalize the words and replace certain parts of the words that we don't want, just incase they sneak in. $crumb_text = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb)); #append folder navigation to link as we foreach through the $crumbs $tree_arr[] = $crumb."/"; foreach($tree_arr as $item){ $tree .= $item; #Create a string of current $tree } #echo out the html for the breadcrumb link echo "<a href='$domain/$tree'>".$crumb_text."</a>"; } } ?> That is the entire code. I creates like a list of directorys and displays them as links, that are in a relevant path to where you are on the site. Please let me know if this is the correct way to do this, thankyou. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 12, 2011 Share Posted April 12, 2011 The "correct way" depends on what you want it to do. If you want it to look at the URL path, transform each component into a nice looking name, and stick it into a breadcrumb, then yes: you're doing it the correct way. Quote Link to comment Share on other sites More sharing options...
Zephni Posted April 12, 2011 Author Share Posted April 12, 2011 Ok thanks, just one thing though. I can't seem to include this file in other pages because it seems to be running before it is included. Like say I am a couple of folders up from /test/breadcrumb/ when i include that script above it seems to only go as far as where the file is sitting, not the current directory. Is there any reason for that? Quote Link to comment Share on other sites More sharing options...
requinix Posted April 12, 2011 Share Posted April 12, 2011 Ok thanks, just one thing though. I can't seem to include this file in other pages because it seems to be running before it is included. Only include that script at the place you want the breadcrumb to appear. Like say I am a couple of folders up from /test/breadcrumb/ when i include that script above it seems to only go as far as where the file is sitting, not the current directory. I don't get that. Where are you? What does the browser's address bar say? What breadcrumb do you get? What did you expect/want to get? Quote Link to comment Share on other sites More sharing options...
Zephni Posted April 13, 2011 Author Share Posted April 13, 2011 (Well there are a couple of folders up from breadcrumb) I was at test/breadcrumb/test2/loldge , in loldge is a file that just has: <?php include("http://www.zephni.com/test/breadcrumb/inc_breadcrumb"); ?> Which has all of the code that makes the breadcrumb. The URL said excatly where I was (" test/breadcrumb/test2/loldge") but the breadcrumb said: Home > Test > Breadcrumb > Index So confused... Thanks for replies Quote Link to comment Share on other sites More sharing options...
requinix Posted April 13, 2011 Share Posted April 13, 2011 You can't include() your own files over the Internet. Use the filesystem path. Quote Link to comment Share on other sites More sharing options...
Zephni Posted April 13, 2011 Author Share Posted April 13, 2011 Oops thanks, sorry didn't know that. Thought it would be ok if on same server. Ok.. so now i have to have: <?php include("../inc_breadcrumb.php"); ?> In the index.php file in the folder above, but what about folders above that, have they all got to have seperate include functions to just put the right amount of "../" before the file name? 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.