This is my homemade PHP encoded solution that works well:
function generateBreadcrumbsForNodeAndDescendants($id) { define ('FOLDER_PATH',dirname(__FILE__).'/'); include(FOLDER_PATH.'db_ajax_connection.inc.php'); // Select node and all subnodes, excepted leafs // This query works, but is possibly not optimized. $req = "SELECT c.id,p.path,c.name FROM `".$database['database']."`.`".$database['prefix']."categories` c LEFT OUTER JOIN `".$database['database']."`.`".$database['prefix']."paths` p ON c.id = p.id WHERE c.isparent AND (c.id=".$id." OR (p.path=".$id." OR p.path LIKE('".$id.".%') OR p.path LIKE('%.".$id.".%'))) ORDER BY p.path ASC"; // We would add the following line to the WHERE clause if we wanted to retrieve leaf nodes too: // OR p.path LIKE('%.".$id."') $res = mysql_query($req) or die(); $descendants = array(); while($descendant = mysql_fetch_assoc($res)) { $descendants[] = $descendant; } $path = ''; // Get the path to the current node. // Because the records from the query are ordered by path, this is the first record. $path = str_replace('.', ',', $descendants[0]['path']); // This is because there is no record stored in the path table for the first-level nodes if ($path=='') $path = '0'; // Find ancestors of the current node $req = "SELECT c.id,p.path,c.name FROM `".$database['database']."`.`".$database['prefix']."categories` c LEFT OUTER JOIN `".$database['database']."`.`".$database['prefix']."paths` p ON c.id = p.id WHERE FIND_IN_SET(c.id,'".$path."')"; $res = mysql_query($req) or die(''); $ancestors = array(); while($ancestor = mysql_fetch_assoc($res)) { $ancestors[] = $ancestor; } // Build a list of all ancestors and descendants of the current node, ie concatenate arrays $nodes = array_merge($ancestors,$descendants); $paths = array(); // Build associative key => value pairs: (id => path) and (id => name) foreach ($nodes as $node) { $paths[$node['id']]=$node['path']; $names[$node['id']]=$node['name']; } $html=''; // for each "descendant" node (including custom "root" node), translate numeric path into breadcrumbs foreach ($descendants as $descendant) { $html .= '<p>'; $path = $paths[$descendant['id']]; if ($path) { // because no path is stored for the 1st level nodes, we must test that $path != '' $i = 0; foreach(explode('.', $path) as $id) { if ($i) $html .= ' > '; $html .= $names[$id]; // These nodes can also be encapsulated in html anchors <a href="">...</a> to become links. $i++; } $html .= ' > '; // optional if we want to add the element to the path of its parent nodes (see below) } $html .= $names[$descendant['id']]; // optional if we want to add the element to the path of its parent nodes // else, we should do some post-processing to remove duplicate paths, // as several nodes may have the same parent path. $html .= '</p>'; } echo $html; } generateBreadcrumbsForNodeAndDescendants((int) $_POST['key']);
Note: by adding OR p.path LIKE('%.".$id."') At the end of the WHERE clause in the first query, you can also get leaf nodes, but in this case, an undefined offset error occurs in the line $path = str_replace('.', ',', $descendants[0]['path']); for leaf nodes, since they have no descendants. Consequently, some code improvement remains possible.
source share