First get your data from database and build an array
$q = mysql_query("SELECT catid, catname, parentid FROM cat");
$pages = array();
while($r = mysql_fetch_assoc($q)) {
$pages[$r['catid']] = array('catname' => $r['catname'], 'parentid' => $r['parentid']);
}
Use a recursive function to build the breadcrumbs:
function build_breadcrumbs($pages, $catid) {
if(is_null($catid) || empty($catid)) return;
return build_breadcrumbs($pages, $pages[$catid]['parentid']).$pages[$catid]['catname'].' > ';
}
The function returns the parent's breadcrumbs + the current breadcrumb. Use the function like this:
$catid = 6;
$breadcrumbs = trim(build_breadcrumbs($pages, $catid), ' >');
echo $breadcrumbs;
Or, if you like arrays more, you can create a function like this:
function build_breadcrumbs($pages, $catid) {
if(is_null($catid) || empty($catid)) return array();
return array_merge(build_breadcrumbs($pages, $pages[$catid]['parent']), array($pages[$catid]['name']));
}
and use it with implode:
$catid = 6;
$breadcrumbs = implode(' > ', build_breadcrumbs($pages, $catid));
echo $breadcrumbs;
That gives you the option to define the separator outside the function. Hope that helps.