Here is how I would do it, I've commented the code so hopefully you can follow it even though it is a little complicated. Jut ask if you have any questions.
I have manually populated the $cats array but you would do it by querying your database and loading all categories into this array. It is structured like this:
$cats[
category_id ] = array ( 'name' =>
category_name, 'parent' =>
parent_id )
This might not scale very well if you have lots of categories, you might have to cache the $cats array to a file. That's the best way I see to avoid speed issues.
CODE
<?php
$cats[1] = array('name' => 'Test1', 'parent' => 0 );
$cats[2] = array('name' => 'Test2', 'parent' => 0);
$cats[3] = array('name' => 'Test3', 'parent' => 2 );
$cats[4] = array('name' => 'Test4', 'parent' => 2);
$cats[5] = array('name' => 'Test5', 'parent' => 3 );
$cats[6] = array('name' => 'Test6', 'parent' => 4);
$cats[7] = array('name' => 'Test7', 'parent' => 6);
// Set category to find parents for
$curr_id = 5;
// Array to store results in
$breacrumb = array();
// Load our current category
$breadcrumb[ $curr_id ] = $cats[ $curr_id ]['name'];
// Make sure we have categories
if ( count($cats) )
{
// Loop while there is a still a parent to be found
while ( $cats[ $curr_id ]['parent'] > 0 )
{
// Loop through all the categories
foreach ( $cats as $id => $info )
{
// Is the parent ID of the category we are looking for
// the same as the ID of the ID of this cateogry?
if ( $cats[ $curr_id ]['parent'] and $cats[ $curr_id ]['parent'] == $id )
{
// It is, so add this to our breadcrumb array
// and set the current category ID to continue
$breadcrumb[ $id ] = $info['name'];
$curr_id = $id;
break 1;
}
}
}
}
// Reverse the array so that the top category appears
// first which is more logical
$breadcrumb = array_reverse($breadcrumb, true);
// Print the breadcrumbs!
print_r($breadcrumb);
?>
All you have to do at the end is loop through the $breadcrumb array outputting a formatted link instead of just doing print_r(). Hope this helps, or at least sets you on the right track.