Jump to content

Breadcrumb with PHP


Zephni

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

(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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.