Jump to content

Dynamic breadcrumb generation for odd folder structure


Merdok

Recommended Posts

Ok I need to add breadcrumbs to a site I am building.

 

I've found a few scripts out there but I think I may need to write my own as a lot of my pages are within subfolders which I do not want to appear on the breadcrumb trail.

 

eg the blog page is linked from the home page as in Home > blog.php , but its place in the server is home > modules > blog > blog.php

 

I want it to appear as the first example.

 

Ideally what I would like is a trail based on site history rather than page depth. So if they user is on blog and they came from the home page it will say 'home > blog > blog.php'

 

Problem is it would need to be dynamic, would it be possible to use document_root but remove all references to the modules folder whilst keeping the links intact?

 

ok I had a bit of a think and I've got most of the way there:

 

$breadcrumbs = $_SERVER['SCRIPT_NAME'];
$breadcrumbs = str_replace('/modules', '', $breadcrumbs);
$breadcrumbs = str_replace('.php','', $breadcrumbs);
$breadcrumbs = str_replace('/',' » ', $breadcrumbs);
$breadcrumbs = 'Home'. $breadcrumbs . ' » ' . $meta_title;

echo $breadcrumbs;

 

which outputs "home > blog > article > article title"

 

However I can't figure out how to get the "blog > article" part to be linked to their respective pages. I thought about using 'explode' on the '$_SERVER['SCRIPT_NAME']' part but I've no idea what it will echo out as, as each page is different.

 

I think I'm stuck guys!

ok I sorted it... its a bit of a dirty way of doing it though. if I can find a cleaner way (or if you have any suggestions) I think it would be better posted on here than this one.

 

but for lack of a better option:

 

 

<?php // Gets the breadcrumbs for the current page$this_page = basename($_SERVER['REQUEST_URI']);if (strpos($this_page, "?") !== false) $this_page = reset(explode("?", $this_page));if ($_SERVER['REQUEST_URI'] != '/') {$breadcrumbs = $_SERVER['SCRIPT_NAME'];$breadcrumbs = str_replace('/socket/modules', '', $breadcrumbs);// Pulls in some data about the installed modulesif ($module_ID) {$modulecheck = "SELECT folder_name FROM core_modules WHERE moduleID =" . $module_ID;$mcheck = mysql_query($modulecheck) or die('Failed to return data: ' . mysql_error());$modArray = mysql_fetch_array($mcheck);extract($modArray, EXTR_PREFIX_ALL, 'dbmod');$breadcrumbs = str_replace('/'.$dbmod_folder_name, '', $breadcrumbs);}$breadcrumbs = str_replace('/'.$this_page,'', $breadcrumbs);$breadcrumbs = str_replace('/','<span class="divider"> » </span>', $breadcrumbs);if ($special_crumb) { $special_crumb = $special_crumb . '<span class="divider"> » </span>';  } //checks for an extra level (specified on page)$breadcrumbs = '<a class="bold" href="'.$siteroot.'">Home</a>'. $breadcrumbs . '<span class="divider"> » </span>' . $special_crumb . $meta_title;} else {$breadcrumbs = 	'<a class="bold" href="'.$siteroot.'">Home</a>';}?>

 

 

Then on the page you wish to call the breadcrumbs:

 

 

<div id="breadcrumbs"><?php echo $breadcrumbs; ?></div>

 

 

Some pages have special requirements (eg some breadcrumbs are within modules and some need their own level added to the trail, in this instance you add the following to the page:

 

 

$module_ID = 2; // Specifies the id of the module being referenced - I don't like this method and will find a better way when I can.$special_crumb = '<a href="'. $siteroot . '/socket/modules/blog/categories.php?cat='.$dbcat_categoryID.'">'.$dbcat_categoryName.'</a>'; // this special crumb returns the name and link to the current articles category

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.