{"authorId":1,"title":{"title":{}},"slug":"dynamic-bread-crumbs","postDate":1182648492,"expiryDate":null,"enabled":1,"fields":{"2":"<p>For a redesign project at work, the new design called for bread crumbs. In my opinion, bread crumbs are a great thing to add to a site, and <a href=\"http:\/\/www.useit.com\/alertbox\/breadcrumbs.html\">others agree<\/a>. I didn&rsquo;t want to have to specify anything on each page for the bread crumbs to work, so I wanted to do it all dynamically.<\/p>\n\n<p>For the project at work, I did it in ColdFusion; but on my own, I did it in PHP. I figured the way that I would have to do it would be to parse the URL to see which folders I was in.<\/p>\n<!--more-->\n<h2 class=\"replaceHeadline\">The Code<\/h2>\n\n<p>File: breadcrumbs.php<\/p>\n\n<div class=\"codeblock\"><code>\n&lt;?php&nbsp;require('buildbreadcrumbs.php');&nbsp;?&gt;<br \/><br \/>&lt;ol&nbsp;id=\"breadCrumbs\"&gt;<br \/>&nbsp;&lt;li&nbsp;class=\"first\"&gt;&lt;a&nbsp;href=\"\/\"&gt;Home&lt;\/a&gt;&lt;\/li&gt;<br \/>&lt;?php<br \/>&nbsp;$breadCrumbs&nbsp;=&nbsp;$_SERVER&#91;'REQUEST_URI'&#93;;<br \/>&nbsp;if($breadCrumbs&nbsp;!=&nbsp;'')&nbsp;&#123;<br \/>&nbsp;&nbsp;$crumbUrl&nbsp;=&nbsp;'\/';<br \/>&nbsp;&nbsp;$toks&nbsp;=&nbsp;split('\/',&nbsp;$breadCrumbs);<br \/>&nbsp;&nbsp;for($i&nbsp;=&nbsp;1;&nbsp;$i&nbsp;&lt;&nbsp;count($toks)&nbsp;-&nbsp;1;&nbsp;$i++)&nbsp;&#123;<br \/>&nbsp;&nbsp;&nbsp;$crumbUrl&nbsp;.=&nbsp;$toks&#91;$i&#93;&nbsp;.&nbsp;'\/';<br \/>&nbsp;&nbsp;&nbsp;if($breadCrumbMapping&#91;$toks&#91;$i&#93;&#93;&nbsp;!=&nbsp;NULL)&nbsp;&#123;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'&lt;li&gt;&lt;a&nbsp;href=\"'.$crumbUrl.'\"&gt;'.$breadCrumbMapping&#91;$toks&#91;$i&#93;&#93;.'&lt;\/a&gt;&lt;\/li&gt;';<br \/>&nbsp;&nbsp;&nbsp;&#125;&nbsp;else&nbsp;&#123;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'&lt;li&gt;&lt;a&nbsp;href=\"'.$crumbUrl.'\"&gt;'.$toks&#91;$i&#93;.'&lt;\/a&gt;&lt;\/li&gt;';<br \/>&nbsp;&nbsp;&nbsp;&#125;<br \/>&nbsp;&nbsp;&#125;<br \/>&nbsp;&nbsp;echo&nbsp;'&lt;li&nbsp;class=\"last\"&gt;'.$pageTitle.'&lt;\/li&gt;';<br \/>&nbsp;&#125;<br \/>?&gt;<br \/>&lt;\/ol&gt;&nbsp;\n\n<\/code><\/div>\n\n<h3 class=\"replaceHeadline\">The Explanation<\/h3>\n\n<p>The first thing that I do, is include the file buildbreadcrumbs.php (I&rsquo;ll get to that later). Then the idea is that we will determine what folders we are in, based on the URL. We split the URL based on the slashes and step through them to make links.<\/p>\n\n<p>Now, back to the File: buildbreadcrumbs.php<\/p>\n\n<div class=\"codeblock\"><code>\n&lt;?php<br \/>$breadCrumbMapping&nbsp;=&nbsp;Array();<br \/><br \/>\/*Folders&nbsp;to&nbsp;be&nbsp;mapped*\/<br \/>$breadCrumbMapping&#91;'f1'&#93;&nbsp;=&nbsp;\"Folder&nbsp;1\";<br \/>$breadCrumbMapping&#91;'f2'&#93;&nbsp;=&nbsp;\"Folder&nbsp;2\";<br \/>$breadCrumbMapping&#91;'f3'&#93;&nbsp;=&nbsp;\"Folder&nbsp;3\";<br \/>?&gt;&nbsp;\n\n<\/code><\/div>\n\n<p>This file is building as associative array to display different text than the folder name. There are some situations where the folders are not too pretty, so basically you just add the folder name (f1, f2, f3) to the associative array and assign it a &ldquo;pretty&rdquo; name (Folder 1, Folder 2, Folder 3). If you don&rsquo;t assign a pretty name to a folder, it will just display the folder name instead.<\/p>\n\n<p>I made <a href=\"http:\/\/trevordavis.net\/play\/breadCrumbs\/f1\/f2\/f3\/\">an example page<\/a> to see the bread crumbs in action.<\/p>\n\n<p>You can download the scripts here:<\/p>\n\n<ul>\n<li><a href=\"http:\/\/trevordavis.net\/images\/content\/2008\/01\/breadcrumbs.phps\">breadcrumbs.php<\/a><\/li>\n<li><a href=\"http:\/\/trevordavis.net\/images\/content\/2008\/01\/buildbreadcrumbs.phps\">buildbreadcrumbs.php<\/a><\/li>\n<\/ul>","4":{"website":{}}}}