Jump to content

Looking For A Simple Php Breadcrumb Script


bjs
 Share

Recommended Posts

Hi,

 

Can anyone recommend a simple php breadcrumb script? I'm looking for something that uses my directory structure, and would look something like this:

 

Home > About > Contact Us

 

I tried the one here:

http://www.zend.com/zend/spotlight/breadcrumb28.php

 

.. but couldn't get it to go more than one level deep.

 

Anyway, I was just wondering if anyone has had any luck with this script or others.

Link to comment
Share on other sites

I found these JavaScript breadcrumbs to be useful: http://webmediaconception.com/de/developme...dcrumbs.en.html

 

They were easy to customize and all u do is add the JS file wherever u want the breadcrumbs to show up like so:

<script language="JavaScript" type="text/javascript" src="js_paths.js"></script>

 

Where js_paths.js is the name of the JavaScript file.

 

hope that helps or at least gives u options...

 

later,

!!blue

 

Link to comment
Share on other sites

  • 1 year later...

Could someone comment on best practices for adding breadcrumb to a LAMP site, specifically:

 

is it better to do it with a function or a class?

with TCH now running PHP 4.4.1, is javascript even necessary to add breadcrumbs to a site?

Is it necessary to write directory tree (folders and file structure) to a mysql table to improve depth and functionality?

 

Zend has 2 breadcrumb navigation scripts listed below:

newer: 0 A Cool Recursive Links Script- path.php http://www.zend.com/codex.php?id=362&single=1 is a 4 star

older: Breadcrumbs navigation http://www.zend.com/codex.php?id=414&single=1 is a 0 star

 

Any comments on these or suggestions to any best practices breadcrumb script?

Link to comment
Share on other sites

I would like to hear about best practices in adding breadcrumb feature to a LAMP site. Maybe we can refine choices to the best out there. Here is a pure PHP script I am using to add breadcrumb navigation. I call this php script with an include on any page which needs to show breadcrumb as such: <p><? include('/breadcrumbgen.php'); ?>

// breadcrumbden.php

<?

/* PHP dynamic link bar script by FranÁois Richardson. Free for use and abuse.

Makes a series of links to the web root (printed as "Home"), each folder

in the file path, and the filename. Underscores in the file names and folder names are

shown as spaces to the visitor, and the file name is displayed without the extension.

You can set the default web page name not to be displayed by assigning a value to the

$def variable. I set this to "index" by default.

 

Making it pretty with CSS: Each link has a class="dynNav" attribute,

in case you need to apply special formatting with CSS. If you create a .dynNav

class, the formatting will override that of any A{....} tag redefinition you do.

The padded slash following each link also has the dynNav class applied to it,

with <span> tags. If you need to apply different formatting to the links

and the slashes, create your style for the slashes as .dynNav, then override it

for the links with an a.dynNav selector.

*/

 

$def = "index"; //default web page for directories on your server.

$dPath = $PHP_SELF; //Get the script path, relative to web root.

$dChunks = explode("/", $dPath); //Separate out folder and file names by looking for slashes.

?>

<a class="dynNav" href="/">Home</a><span class="dynNav"> >> </span> <!-- make a leading "home" link -->

<?

 

for($i=1; $i<count($dChunks); $i++){ //PHP arrays are 0 inxeded but we are skipping the first element because of the way explode() works.

echo("<a class=\"dynNav\" href=\"/"); //Make each chunk a link.

for($j=1; $j<=$i; $j++){ //Subloop to create the path for each chunk.

echo($dChunks[$j]); //Write each piece of the chunk's path.

if($j!=count($dChunks)-1) echo("/");//If that piece was a folder name, append a slash.

}

if($i==count($dChunks)-1) { //If the chunk is a file, not folder name...

$prChunks = explode(".", $dChunks[$i]); //take out the file extension...

if ($prChunks[0] == $def) $prChunks[0] = ""; //don't display the filename if it's index or whatever default you specified.

$prChunks[0] = $prChunks[0] . "</a>"; //add the closing tag.

} else

$prChunks[0]=$dChunks[$i] . "</a><span class=\"dynNav\"> / </span>"; //Otherwise, just use the chunk name, close the a tag and add a paddeslash for display.

echo('\"\>');

echo(str_replace("_" , " " , $prChunks[0])); //Finish writing the link, replacing underscores with spaces for the end user.

}

?>

Link to comment
Share on other sites

  • 2 weeks later...

i like this one.

you'll find it at:

http://www.drweb.de/php/brotkrumen.shtml

 

><?php
$an="you are here: ";
$home="http://www.drweb.de/"; // your domain 
$pie=explode("/","$_SERVER[PHP_SELF]");
$tr=" > "; // Alternate: ':', '/'
$b=count($pie);
echo $an."<a href=\"".$home."\">Startseite</a>"; // Alternate: 'Home', 'Start'
for($a=1;$a<$b-1;$a++){
$ta=$ta.$pie[$a]."/";
echo $tr."<a href=\"".$home.$ta."\">".ucfirst($pie[$a])."</a>";}
$file=explode('.',ucfirst($pie[$b-1]));
echo "<b>".$tr.$file[0]."</b>";
?>

Edited by weissi
Link to comment
Share on other sites

Is there a way to put these breadcrumbs into your site using includes?

ie, I dont Imagine that I could put it in the include that is right under my logo. I would imagine that it would make the breadcrumb the same for everypage (home/includes/crumb.php). Is that right?

Link to comment
Share on other sites

So we have dialogue, Thank you all for stopping by. I was beginning to think that script talk was dead at TCH

Shouldn't $dPath = $PHP_SELF; be $dPath = $_SERVER['PHP_SELF']; ??
carbonize,

$PHP_SELF = $_SERVER['DOCUMENT_ROOT'] if register_globals is set to on, u can check your setting at www.yoursite.com:2082/phpinfo.php

 

weissi,

I like how cpmpact your code is, 12 lines versus 22 lines in my improved FBD script, shown below. Both scripts are identical in how they strip out "/" from URI Path and the file extensions ".php" etc. using two explode() functions. The FBD script however offers the following advantages for those additional 10 lines of code, namely:

  1. The ability to turn the current file into a link and not just the heirechal structure in the breadcrumb.
  2. The ability to hide certain filenames within folders from the breacrumb by defining those in $def variable.
  3. The ability to turn underscores in file names to spaces in the breadcrumb.
  4. The ability to use CSS to style both links and link separator in the breadcrumb using class .dynNav
><?
/* PHP dynamic link bar script FranÁois/Brett/Deano, AKA FBD, script
set root with $def variable
set Home link with $rootlink variable
set separator with $separator variable
set link style with CSS class a.dynNav
set separator style with CSS class .dynNav 
*/
$rootlink = "Home";						   // define root link
$separator = ">>";							// define separator
$def = "index";							   //default web page for directories on your server.
$dPath = $PHP_SELF;						   //Get the script path, relative to web root.
$dChunks = explode("/", $dPath);   		   //Separate out folder and file names by looking for slashes.
$dChunkcount = count($dChunks);
?>
<a class="dynNav" href="/"><? print $rootlink ?></a> <span class="dynNav"><? print $separator ?> </span>  <!-- make a leading $rootlink -->
<?
for($i=1; $i<$dChunkcount; $i++) { 			   //PHP arrays are 0 inxeded skipping the first element because of how explode() works
echo("<a class=\"dynNav\" href=\"/");		 //Make each chunk a link.
	for($j=1; $j<=$i; $j++) { 				//Subloop to create the path for each chunk.
	  echo($dChunks[$j]); 					//Write each piece of the chunk's path.
	  if($j!=$dChunkcount-1) print "/";	   //If that piece was a folder name, append a slash.
	} 
if($i==$dChunkcount-1) {					  //If the chunk is a file, not folder name...
$prChunks = explode(".", $dChunks[$i]);	   //take out the file extension...	
if ($prChunks[0] == $def) $prChunks[0] = "";  //don't display the filename if it's index or whatever default you specified.
$prChunks[0] = $prChunks[0] . "</a>"; 	   //add the closing tag.
} else 
$prChunks[0]=$dChunks[$i] ."</a><span class=\"dynNav\"> ". $separator." </span>"; //Otherwise, just use the chunk name, close tag and add a paddeslash for display.
echo "\">";
echo(str_replace("_" , " " , $prChunks[0])); //Finish writing the link, replacing underscores with spaces for the end user
}
?>

 

wampthing,

you absolutely can place your breadcrumb script in an includes folder , say /home/includes/crumb.php and call it up on any page by simply adding the following command:

><?php include $_SERVER['DOCUMENT_ROOT']."/includes/crumb.php"; ?>
Link to comment
Share on other sites

carbonize,

$PHP_SELF = $_SERVER['DOCUMENT_ROOT'] if register_globals is set to on, u can check your setting at www.yoursite.com:2082/phpinfo.php

 

Yes but my point is you should never assume. It is always best to code for global variables being switched off even more so as that is now the default. I doubt any TC server has globals switched on nor would any other host who has half a clue leave them switched on.

Edited by carbonize
Link to comment
Share on other sites

So I just installed the script, and am in fact having it show only the location of the included file. I wonder if it is because my site is done with .shtml with .htm files embeded in it instead of html with php. Any ideas anyone. (thanks btw for your response deanavail)

Link to comment
Share on other sites

carbonize, you are absolutely right, never never assume.

wampthing, it should work fine,

  1. can you recheck that you copied script verbatim and save it as crumb.php in your www.sitedomain.com/includes/crumb.php
  2. edit a new page and place <?php include $_SERVER['DOCUMENT_ROOT']."/includes/crumb.php"; ?> at the top
  3. add <? echo "testing 123" ;?> right below it
  4. save page as test.php right below root and upload
  5. browse this test page www.sitedomain.com/test.php and what do u see?

although I have not tested php code in .shtml, you have to tell php to parse within .shtml files. This is likely done in your php.ini file, check with TCH support. good luck

Link to comment
Share on other sites

You tell the server to send shtml files to PHP via the .htaccess file. php.ini is just where PHP keeps it's settings. It would be in Apaches config file where you would specify what files are treated as what and the .htaccess can be used to override this.

Link to comment
Share on other sites

I tried adding .shtml to the following

 

>XBitHack on
AddType application/x-httpd-php .htm .html

 

It messed up the pages. It forced them to be saved to your hard drive. I had a similar problem before and came away with the understanding that .shtml wasnt a very good format to be including php files.

 

Perhaps I am missing something.

Link to comment
Share on other sites

If you add .shtml to

AddType application/x-httpd-php .htm .html

as in

AddType application/x-httpd-php .htm .html .shtml

you can include php in .shtml

and you do not need the

XBitHack on

 

but you give up the other includes

like

<!--#include virtual="/footer.html" -->

Link to comment
Share on other sites

I haven't used .shtml pages since coming to TCH. Yes, my old host required .shtml extensions to use #include statements.

 

Once I moved the accounts here I switched them all back to .htm and .html and have even changed one site to .php.

 

It's a task if you have a lot of pages but in the long run well worth it.

Link to comment
Share on other sites

but you give up the other includes

like

<!--#include virtual="/footer.html" -->

Maybe that is my problem. I am using the <!--#include virtual="/footer.html" --> include. It sounds like I would need to change all of the previous includes to one of the following;

<?php include $_SERVER['DOCUMENT_ROOT'].""; ?>

 

<?php include(); ?>

Is that right.

Perhaps that is what is causing my errors when I have .shtml included in my htaccess file. AddType application/x-httpd-php .htm .html .shtml

Link to comment
Share on other sites

Yes that is the reason the other includes do not work now.

 

I prefer

<?php include $_SERVER['DOCUMENT_ROOT']."/header.php"; ?>

 

 

With the php includes you can use other tricks like a different title on each page.

 

 

the top of a page starts like this

<?php

// add header and additional title

$page_title="home";

include $_SERVER['DOCUMENT_ROOT']."/header.php";

// begin main content

?>

 

then in header.php the title is dispayed as

My Web Site - <?php print("$page_title"); ?>
Link to comment
Share on other sites

I went and changed the links as mentioned. The site still gave me errors, so I just went and changed it so that I was including PHP files in the html. I am done with .shtml (on that site anyway)

 

And to get back to the point of this thread, the breadcrumb scripts is working dandy.

Link to comment
Share on other sites

Well if you are worried about people not being able to find your pages after you change them to .php you could always use your .htaccess to send them to the correct page. Or better yet write a smart 404 page using PHP to explain the reason for the changes and give them a link to the correct url for the page they requested.

Link to comment
Share on other sites

Well if you are worried about people not being able to find your pages after you change them to .php you could always use your .htaccess to send them to the correct page. Or better yet write a smart 404 page using PHP to explain the reason for the changes and give them a link to the correct url for the page they requested.

 

I know how to redirect specific files, and anything in a specific folder. I dont know how to redirect an extension of .shtml to the same url, but endind with php.

 

Id like to do it via htaccess for time sake. Any Ideas?

Link to comment
Share on other sites

Off the top of my head no but it would be a case of using regex. If nobody has answered by the time I get home I'll throw something together. The smart 404 page is the better option though as it informs the user of the change incase they have bookmarked the page.

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

 Share

×
×
  • Create New...