#native_company# #native_desc#
#native_cta#

Breadcrumbs Navigation

By Russell Michell
on July 21, 2002

Version: 1.0

Type: Function

Category: Other

License: GNU General Public License

Description: A PHP function *without* database, that displays a clickable, breadcrumbs style navigation item: home > gallery > my gallery

<?php
/*-----------------------------------------------------------------------------------
#Filename: trail.txt
#Author and format of any credits: "Russ Michell 2002 [email protected] - www.theruss.com"
#Status: Freeware - that means you can use this script wherever you want for nowt. 
#Please ensure credits are intact or pasted into wherever you wish to use this code.
#I, Russ Michell, take no responsibility whatsoever for *any* problems arising through
#the use/deployment of this code.
#Enjoy! Russ.
-------------------------------------------------------------------------------------*/

//Calling the function and echoing to the page:
//---------------------------------------------

#1). Call the genTrail() function in each page thus: (For a STATIC page)
$pageTrail = "About this site";
genTrail('index','.php',$pageTrail,'','');

#2). Call the genTrail() function in each page thus: (For a DYNAMIC page)
$pageTrail = "gallerys";
genTrail('index','.php',$pageTrail,$galleryID,$GalleryName);

//Function to abbreviate the text that the genTrail() function below prints:

//Name: "abbr()" Abbreviate text string for display purposes
//Arg1: '$str' the test string to be shortened
//Arg2: '$len' the length the string should be after shortening
function abbr($str,$len)
{
	//Next two lines removes any html tags:
	$str = htmlspecialchars($str);
	//Only if the length of $str is > $len, will $str require shortening:
	if (strlen($str) > $len)
	{
		$str = substr($str,0,$len) . '...';
	}
	return $str;
}

//The gentrail() function itself:

//Name: genTrail() - Generate a trail for breadcrumbs navigation.
//Arg1: '$index' A string - the name of your index page: index,default,home etc
//Arg2: '$suf' A string - the file suffix of site's pages: .php,.htm,.html etc
//Arg3: '$pageTrail' A string of the format: "about|about<sublevelname> (if used)";
//Arg4: '$query1' An [optional] string: if $query1 then display on basis of $query2
//Arg5: '$query2' An [optional] string: if $query1 then display $query2
function genTrail($index,$suf,$pageTrail,$query1,$query2)
{
	//Used when function is called from an include/library file
	global $trail;
	//If a variable needs to be set to display DB content, deal with it:
	if($query1)
	{
		$pageTrail = "$pageTrail|$query2";
	}
	else
	{
		$pageTrail = "$pageTrail";
	}
	//Initiate the $trail variable text:
	$trail = "<strong class='trail'>you're @:</strong>&nbsp;<a title='Home.' href='$index$suf'>home</a>";
	
	//Create an array to loop thru from '$pageTrail':
	$array = explode("|",$pageTrail);
	
	//Get last element & remove from array (displayed later at end of trail)
	$last = strtolower(array_pop($array));
	
	//Loop thru array and add re-formatted last element ('$lastElement') to $trail:
	foreach($array as $val)
	{
		//Make all displayed text, lower case:
		$val = strtolower($val);
		
		//Check $pageTrail for whitespace ('about us' for example) use first as link:
		if(preg_match("/ /",$val))
		{
			$valArr = explode(" ",$val);
			//Take first element as the link text:
			$trail .= "&nbsp;&gt;&nbsp;<a title='$val.' href='$valArr[0]$suf'>" . abbr($val,25) . "</a>";
		}
		else
		{
			$trail .= "&nbsp;&gt;&nbsp;<a title='$val.' href='$val$suf'>" . abbr($val,25) . "</a>";
		}
	}
	
	//Display trail to page (including a '>') & add last element ('$last') to $trail:
	$trail .= "&nbsp;&gt;&nbsp;<strong class='trail'>" . abbr($last,25) . "</strong>";
	return $trail;
}

?>