PHP Breadcrumbs
A simple class for cleanly organizing breadcrumbs for pages in PHP.
<?php /* * Create an array for each page which contains (in order from left to right) * all the pages in its breadcrumbs. Array should be in format: "Title"=>"URL" * or "Title"=>self::getPage() if it should have a drop-down menu. * * Then add to the getPages() function a mapping of the page name to the array name. * e.g. "iceCream.php"=>self::$iceCream * * Arrays can have key of "_" to represent the name of this particular array. This is * used for drop-down menus. * * If you need a content-specific title or an id in the URL, use %PAGE_TITLE% and * %ID% as placeholders. * * On the page you want the breadcrumbs on, just place this code: * echo BreadCrumbs::getBreadCrumbs(basename($_SERVER['SCRIPT_FILENAME'])); */ class BreadCrumbs{ static $separator = " > "; static function getHome(){ return array( '_'=>'index.php', 'Home'=>'index.php', 'Members'=>'members.php', 'About'=>'about.php', ); } static function getMembers(){ return array( "Home" => self::getHome(), "Members" => "members.php" ); } static function getMember(){ return array( "Home" => self::getHome(), "Members" => "members.php", "%PAGE_TITLE%" => "member.php?id=%ID%" ); } static function getAboutPages(){ return array( "_" => "about.php", "About Us" => "about.php#AboutUs", "Contact" => "about.php#Contact", "Affiliates" => "about.php#Affiliates", ); } static function getAbout(){ return array( "Home" => self::getHome(), "About" => self::getAboutPages() ); } static function getRegister(){ return array( "Home" => self::getHome(), "Register" => "register.php" ); } static function getPages(){ return array( "about.php"=>self::getAbout(), "member.php"=>self::getMember(), "members.php"=>self::getMembers(), "register.php"=>self::getRegister(), ); } /********************************************/ static function getHyperLink($name, $link, $page, $id=0, $title=""){ $color = strtok($link, '?')==$page ? "class='breadcrumb_active' ":"class='breadcrumb' "; $name = str_replace('%PAGE_TITLE%', $title, $name); $link = str_replace('%ID%', $id, $link); return "<a {$color}href='$link' title='$name'>$name</a>"; } static function getLinkDropDownList($name, $list, $current_page){ $color = strtok($list['_'], '?')==$current_page ? "class='breadcrumb_active' ":"class='breadcrumb' "; //is this the current page? $result =<<<EOF <dd class='breadcrumb_main'> <a href="{$list['_']}" $color onmouseover="document.getElementById('{$name}Menu').style.display='';" onmouseout="document.getElementById('{$name}Menu').style.display='none';" style="padding-left:10px;">$name</a> <div id="{$name}Menu" style='display:none' class='breadcrumb_sub_menu' onmouseover="this.style.display='';" onmouseout="this.style.display='none';">\n EOF; foreach($list as $listName=>$listLink){ if($listName=='_')continue;//this is the main link $result .= " <div id='contr' class='breadcrumb_sub_menu_item'><a href='$listLink'>$listName</a></div>\n"; } $result .= " </div> </dd>\n"; return $result; } static function getBreadCrumbs($page, $id=0, $title=""){ $crumbs = ""; $id = (int)$id; $title = htmlentities($title, ENT_QUOTES, 'utf-8'); $allPages = self::getPages(); if(array_key_exists($page, $allPages)){ $pageArray = $allPages[$page]; foreach($pageArray as $name=>$link){ if(is_array($link)){ $crumbs .= self::getLinkDropDownList($name, $link, $page); }else{ if($name=='_')continue;//this is just for drop-down menus $crumbs .= self::$separator; $crumbs .= self::getHyperLink($name, $link, $page, $id, $title); } } } return $crumbs; } } /* Styles: .breadcrumb, .breadcrumb_active{ padding-right:10px; } .breadcrumb_main { background:url(../images/siemens/comon_arrow.gif) no-repeat; background-position:14px 4px; float:left; padding-left:16px; } .breadcrumb_sub_menu{ background-color:#fff; position:absolute; z-index:9999; min-width:114px; max-width:214px; padding-top:5px; border-left:1px solid #FFFFFF; border-right:1px solid #FFFFFF; border-bottom:1px solid #FFFFFF; } .breadcrumb_sub_menu_item{ background:url(../images/siemens/comon_arrow.gif) no-repeat; background-color:#EEEEEE; border-top:1px solid #666666; border-bottom:1px solid #666666; background-position:10px 5px; padding-left:20px; padding-right:10px; padding-top:2px; padding-bottom:2px; min-width:114px; max-width:214px } */ ?>

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Download this code in plain text format here
Comments: