Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > All Around Tutorials > Serverside Scripting Tutorials
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 11-11-2006, 11:50 PM
  #1
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,859
iTrader: (0)
erisco will become famous soon enougherisco will become famous soon enough
Making a breadcrumb in PHP

Breadcrumb script download
It was too big to have posted here!

Usage and Examples

If you have not already, download the breadcrumb script and take a look at the documentation already included in the README. Many of the options left at default will be fine. I will explain the usage of what are likely to be the popular options. The descriptions provided in the download should suffice for the ones I do not talk about, however. To get a feel as how customization works, try calling the function without anything special like this:

PHP Code:
<?php
echo breadcrumb();
The echo, or print, is required because breadcrumb() returns a string. It does not output the breadcrumb itself. Now, lets change the separator by changing the separator option.

PHP Code:
<?php
echo breadcrumb(array('separator' => '/'));
Now, instead of the double angle bracket, a forward slash appears between each link. By default the link back to the root of your site is called Home. We can change this through the home_link option.

PHP Code:
<?php
echo breadcrumb(array('home_link' => 'Index''separator' => '/'));
Now the first link appears as Index rather than Home. Note, the order of the options does not matter. We could have also defined the options in an associative array assigned to a variable, and then passed that variable to the function. This will be ideal if you have many options to change.

Often, sites have extensions on the URIs such as /page.php, where .php is the extension. This will produce a link in our breadcrumb that will look like page.php, which is sometimes undesirable. We can have the extension removed by setting the rm_file_ext option to true.

PHP Code:
<?php
echo breadcrumb(array('rm_file_ext' => true));
The last, most useful, options are the templates. These specify how each link is outputted. This is useful if you are not necessarily outputting HTML, or you need to add a class to each anchor tag, or whatever the case may be. We have three templates to make use of: the "first_tpl", the "tpl", and the "last_tpl". The first_tpl is applied to the first link in the breadcrumb, which is always your home link. The tpl template is applied to every link after that except for the last link, where the last_tpl is used. Typically, you only care about about the "tpl" template.

PHP Code:
<?php
$options 
= array('tpl' => '<a href="%s" class="breadcrumb_link">%s</a>');
echo 
breadcrumb($options);
Each template uses string formatting to specify where the link and where the link text are inserted. If you are not familiar with string formatting, check out http://php.net/sprintf. If you need to insert the values in reverse order, or you need to insert each one multiple times, make use of numbered placeholders. The link is argument 1, and the link text is argument 2. Example:

PHP Code:
<?php
$options 
= array('tpl' => '<a href="%1s" onclick="return popup(\'%1s\')">%2s</a>');
echo 
breadcrumb($options);
Where first_tpl and last_tpl come into use may be with CSS styling. Let's pretend that we want dividers between each breadcrumb link, so we use the border-left css property to achieve this. However, it looks silly to have the first link with a border to the left of it. We can repair this by using first_tpl to specify it has a class named "first_link" that sets the width of the left border to zero.

PHP Code:
<?php
$options 
= array('first_tpl' => '<a href="%s" class="first_link">%s</a>');
echo 
breadcrumb($options);
And that concludes the mini-tutorial of how to use my breadcrumb function. If you are still confused, please make sure you have read the documentation included in the README. If that does not help, feel free to leave a post below and I will try to answer your question the best that I can.

New features!

Package Update Dec 27, 2008: The documentation is now in a README and the breadcrumb.php file received a few changes. None should break BC other than the new default formatting. If this new default formatting is in the way, please let me know.

Feature Added Dec 27, 2008: You can now provide a callback to format your link text. By default a formatter that replaces hyphens and underscores with spaces and capitalizes all words is included. If you do not want this functionality, pass a callback that simply returns the string unchanged. This new option is called "format_callback". Ex:

PHP Code:
<?php
$format 
create_function('$s''return $s;');
echo 
breadcrumb(array('format_callback' => $format));
This should address issues raised by pschena.

Where did the old post go???
This breadcrumb function has received a fair bit of popularity and I felt it necessary to address a few issues I have been getting emails about.

This script does not adapt to all breadcrumb schemes.

When I originally wrote the breadcrumb function (the ones that used to be listed below, removed in favour of the new version) I had a fair bit less experience with PHP than I do now. I fixed most of issues with the function's design with my "new breadcrumb script" which was offered via download from my web site as a zip (a link used to be here, still is). However, the primary issue is that it is not practical to create a function to adapt to every breadcrumb scheme. While I offered quite a bit of configuration, there is bound to be case after case where it isn't enough. For simple, run of the mill PHP powered web sites, this function will likely do its job. For more complex applications it is likely to hit some snags. So keep in mind that while I attempted to cover a reasonable amount of possibilities, I did not cover every single one. If you are struggling getting my function configured to work for you, it may just be easier to create your own solution.

One of the biggest hurdles for using my breadcrumb function is if the URI does not actually reflect the breadcrumb you want to generate. The data that should be used to generate the breadcrumb may come from other sources. This is quite common in sites using rewriting or get or post data to pull up pages. For example, consider this:

http://www.example.com/pages.php?page=portfolio

The best breadcrumb you are going to get is something like:

Home >> pages

Which is obviously not what you want. The links will work properly, but the link text is suboptimal. I could add an option that would allow you to specify something like:

PHP Code:
array('pages' => 'Portfolio'); 
But that will only work in situations where the number of hooks given by the URI is equal to the amount of links you really need in the breadcrumb. Let's consider this scheme:

http://www.example.com/view.php?catid=493

However, category 493 is actually a sub category of a sub category of a category. So while we could use the config array to turn

Home >> view

Into...

Home >> LCD Monitors

But we really need...

Home >> Electronics >> Displays >> LCD Monitors

Now we have yet another problem. Not only are there not enough hooks, there is no way to specify the links associated with them. You could specify the needed breadcrumb as an associative array, perhaps, such as:

PHP Code:
array('Home' => '/',
'Electronics' => '/view.php?catid=200',
'Displays' => '/view.php?catid=302',
'LCD Monitors' => '/view.php?catid=493'); 
However, if this were the case, you could easily transverse the array yourself. By the time you solve the issues associated with my limited breadcrumb function, you find yourself not needing of it anyhow. The function is targeted towards a particular use-case where the data needed for the breadcrumb is in the URI. For example, HTMLForums's URI scheme should work fine because of their use of URL rewriting. Our /view.php?catid=xxx example could be fixed with rewriting, and we could have /electronics/displays/lcd-monitors/. The breadcrumb function would work fine with that as well. Such uses of rewriting are also very beneficial to SEO. Not every rewriting scheme is that simple though, and it will not always result in a form that works nicely with my breadcrumb script. Again, it is not practical to adapt to every rewriting scheme either.

That said, if you have any suggestions, improvements, or questions for this script please let me know via private message or a post below. I hope that this script continues to solve many breadcrumb'ing needs.

Last edited by erisco : 12-27-2008 at 11:06 AM. Reason: Resolving old issues
erisco is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 11-17-2006, 11:34 AM
  #2
platey
Novice (Level 1)
 
Join Date: Nov 2006
Posts: 3
iTrader: (0)
platey is an unknown quantity at this point
Talking

Beautiful PHP!

this is exactly what ive been looking for, thank you!

my only questions regarding this code -

is there anyway you can code it to hide or take off the .php suffixes when it displays it back to the viewer/user? which leads me towards this question: on the Home page is there any way that the code can take away the index file rather than have it display this 'Home / index.php'??

also, my css doesnt seem to have any affect to the script, regarding positioning, once pulled in.

thanks again though!
platey is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 11-18-2006, 11:31 AM
  #3
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,859
iTrader: (0)
erisco will become famous soon enougherisco will become famous soon enough
Here is a script with your desired modifications

Edit:
Removed -- script was not robust enough to continue publishing. Use the new version. Download link is in the OP.


I didn't put the addons into the passed arrays, I didn't feel there would be a need to because all servers should only have one permitable filename for your index/home/default or whatever. Also most web masters or developers typically keep the same file extensions and don't mix up .php and .html and .htm and the whatnot.

If you want your CSS to work with it, try this call:

PHP Code:
echo '<span class="breadcrumb">'.breadcrumb().'</span>'
Now in your CSS you can do the following:

Code:
.breadcrumb A:link, .breadcrumb A:visited, .breadcrumb A:hover, .breadcrumb A:active {
text-decoration:underline;
color:#333;
}
.breadcrumb A:hover {
color:#990000;
}
Just for example. You can output whatever you want around the breadcrumb for customized appearance.

I hope this helps!

Last edited by erisco : 12-26-2008 at 02:36 PM.
erisco is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 11-20-2006, 04:34 AM
  #4
platey
Novice (Level 1)
 
Join Date: Nov 2006
Posts: 3
iTrader: (0)
platey is an unknown quantity at this point
Seriously erisco, this is brilliant!

Thank you for this, and i hope someone else will stumble across this code and find it as useful as i did!
platey is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-21-2007, 05:39 PM
  #5
Mac
SuperHero (Level 14)
 
Join Date: May 2001
Location: SF, CA
Posts: 278
iTrader: (0)
Mac is on a distinguished road
I literally just stumbled upon this code....absolutely brilliant.
__________________
Mac
Mac is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-21-2007, 06:13 PM
  #6
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,859
iTrader: (0)
erisco will become famous soon enougherisco will become famous soon enough
Great to see the script came into good use! Thank-you for the compliments. To better the script I sped up a couple of the strings, but the main improvement was dropping the need for preg_replace() to slice off query strings which will use the regex engine. Although the new alternative using a few more, yet faster, functions isn't as perfect it will certainly still do the job. If anyone has problems with this change please let me know and I can rework a solution. Also keep note that only the original script changed and not the one platey requested.

Thanks!
erisco
erisco is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-24-2007, 10:42 AM
  #7
robin1
Novice (Level 1)
 
Join Date: Mar 2007
Posts: 1
iTrader: (0)
robin1 is an unknown quantity at this point
breadcrumb help

Hello,
I'm new to php and i'm working on a small classified script for my website. I'm trying to add the script you posted here and could use some help.
right now i'm testing the scrip on my local PC(apache,php,mysql).

the script works awesome but not sure how to modify to fit my classified script.
folder the php scripts are located: http://192.168.0.101/classified/

http://192.168.0.101/classified//index.php , i added the breadcrumb function:
breadcrumb('Classified', ' >> ', false, false)

after running it it displays the the following:
Classified >> Classified >>
how do i just display the classified rather the the url and classified i entered in the breadcrumb?

sec problem:
when i click on a category it goes to page_2 (as it should) but it displays:
page2 >> Classified >> Page_2.php?subcat=Housing
it moves page2 to the beginning and classified(which is the main page) to the sec spot?

any help you can give me would be greatly appreciated.
Thanks
robin

Last edited by robin1 : 03-24-2007 at 10:44 AM. Reason: for clearfication in my description
robin1 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-24-2007, 02:19 PM
  #8
Paul
Super Deity (Level 18)
 
Paul's Avatar
 
Join Date: Mar 2001
Location: 127.0.0.1
Posts: 4,035
iTrader: (0)
Paul has a spectacular aura aboutPaul has a spectacular aura about
Looks like google picked up on this, just search for breadcrumb PHP and this post comes up toward the top. Congrats erisco, you are famous .
__________________

Pawel Kowalski
Albuquerque Web Design

templatesXchange.com - Free Web Templates - Native American Jewelry
Paul is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-25-2007, 11:21 PM
  #9
johnz
charlie dont surf!
 
johnz's Avatar
 
Join Date: May 2006
Location: Long Island, New York
Posts: 1,239
iTrader: (0)
johnz will become famous soon enough
Great stuff erisco! I'm sure this will be helpful for many people.
__________________
work. collegehumor.com
blog. johnzanussi.com


If someone has helped you be sure to add to their reputation
johnz is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 04-02-2007, 09:43 PM
  #10
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,859
iTrader: (0)
erisco will become famous soon enougherisco will become famous soon enough
robin1, the script relies on splitting the url at forward slashes. In the url http://192.168.0.101/classified//index.php there is a double forward slash which is probably the culprit. The other problems may have cascaded from this.

If the url must contain two forward slashes, for whatever reason, do a realpath() call on $_SERVER['REQUEST_URI'] so that the line looks like this:

PHP Code:
    $whole $_SERVER['HTTP_HOST'].realpath($_SERVER['REQUEST_URI']); 
And that should clear it up.

Paul, wow, that is pretty neat I don't think I have ever had something less than six keywords long appear on the first page of Google before heheh.

johnz, thanks, happy PHP'ing!
erisco is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 04-28-2007, 03:18 PM
  #11
sugar_lisx
Novice (Level 1)
 
Join Date: Apr 2007
Posts: 1
iTrader: (0)
sugar_lisx is an unknown quantity at this point
hey, That code is the only code that works for me!
Is there a way of labeling the directories? So it doesn't come up as the file name? If that makes sense?
thanks
sugar_lisx is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 04-29-2007, 06:45 PM
  #12
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 31,059
iTrader: (0)
scoutt is just really nicescoutt is just really nicescoutt is just really nicescoutt is just really nice
Just to note erisco, and for anybody that uses IIS with php, this script will not work. Apache is required for it to work. The reason is that IIS does not populate $_SERVER['DOCUMENT_ROOT'], which will fail in this script.

Otherwise good job erisco.
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 05-02-2007, 09:09 PM
  #13
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,859
iTrader: (0)
erisco will become famous soon enougherisco will become famous soon enough
Sorry, here is a fix:

PHP Code:
<?php
if (!isset($_SERVER['DOCUMENT_ROOT'])) {  
        
$_SERVER['DOCUMENT_ROOT'] = substr($_SERVER['SCRIPT_FILENAME'] , , -strlen($_SERVER['PHP_SELF'])+);
}
?>
Which was a solution taken from: http://ca3.php.net/reserved.variables
So, if this one doesn't work, there are more on there.

Damn IIS
erisco is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 05-04-2007, 02:24 PM
  #14
Miriam
Novice (Level 1)
 
Join Date: May 2007
Posts: 1
iTrader: (0)
Miriam is an unknown quantity at this point
Thank you, this script is great. I have only one question. I have my index.php in the root and all other files are in folders. The breadcrumb script prints also the name off the folder. Is there a way to not show the foldername in the breadcrumb?

Example:
I have a file contact.php in the folder pages. The breadcrumb prints: Home>pages>contact

I want the pages removed from the printout, but not from the link offcourse. Is this possible?
Miriam is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 05-08-2007, 08:42 AM
  #15
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,859
iTrader: (0)
erisco will become famous soon enougherisco will become famous soon enough
sugar_lisx, you mean "masking" the real directory names with something different?

Miriam, yes. The portion...
PHP Code:
$whole $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
Defines what is split up into the braedcrumb. In your case you would define it yourself and exclude pages/.
PHP Code:
$whole 'http://localhost/'.$page
Where $page is the name of the page you are loading out of pages/... however that is specified.
erisco is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Reply
KEEP TABS
SPONSORS
 
Boxedart

 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 02:11 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.