sallieann Posted January 30, 2009 Share Posted January 30, 2009 Hi, I'm trying to build a breadcrumb trail which displays all parents to any particular category. For example; Fieldwork Suppliers - United Kingdom - Scotland - Glasgow Each category is stored in the database with a name, id and parent. I'm using the following to call the function; $pageTitle = getCatPageTitle($cat->name, $cat->parent, $catid); and the function is as follows; function getCatPageTitle($catName, $parentId, $catId) { global $database; $sql = "SELECT id, name, parent from dir_category"; $database->SetQuery($sql); $rows = $database->loadObjectList(); $titleArray[0] = $catName; foreach ($rows as $row){ if($row->id==$parentId) { $titleArray[] = $row->name." - "; $parentId = $row->id; } } Currently it is only outputting; Scotland - Glasgow Can anybody explain why it isn't looping correctly? Any help greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/143130-solved-how-to-get-all-parents-in-a-hierarchical-category-structure/ Share on other sites More sharing options...
premiso Posted January 30, 2009 Share Posted January 30, 2009 You need to use recursion to have this trickle down and display the breadcrumb. function getCatPageTitle($catId, $titles=array()) { global $database; if ($catId == 0) return; // no more to display; $sql = "SELECT id, name, parent from dir_category WHERE id = '$catId'"; $database->SetQuery($sql); $rows = $database->loadObjectList(); foreach ($rows as $row){ $titles[] = getCatPageTitle($row->parent, $titles); $titles[] = $row->name; } return $titles; } $titles = getCatPageTitle(6); echo implode(" - ", $titles); That is untested but is the gist of what you need to do. Look into recursion. EDIT: Forgot the return variable lol, added that in. Quote Link to comment https://forums.phpfreaks.com/topic/143130-solved-how-to-get-all-parents-in-a-hierarchical-category-structure/#findComment-750630 Share on other sites More sharing options...
sallieann Posted January 30, 2009 Author Share Posted January 30, 2009 Thanks for your response. I'll give it a try and let you know how it goes. Quote Link to comment https://forums.phpfreaks.com/topic/143130-solved-how-to-get-all-parents-in-a-hierarchical-category-structure/#findComment-750634 Share on other sites More sharing options...
premiso Posted January 30, 2009 Share Posted January 30, 2009 Well, I looked at my code and found some flaws. Let's try this: function getCatPageTitle($catId, $title='') { global $database; if ($catId == 0) return $title; // no more to display; $sql = "SELECT id, name, parent from dir_category WHERE id = '$catId'"; $database->SetQuery($sql); $rows = $database->loadObjectList(); foreach ($rows as $row){ $titles[] = getCatPageTitle($row->parent, $row->name); } return $titles; } $titles = getCatPageTitle(6); echo implode(" - ", $titles); Give that one a try, I think that is correct now. Untested still. Quote Link to comment https://forums.phpfreaks.com/topic/143130-solved-how-to-get-all-parents-in-a-hierarchical-category-structure/#findComment-750637 Share on other sites More sharing options...
premiso Posted January 30, 2009 Share Posted January 30, 2009 Alright, I actually tested this one: function getCatPageTitle($catId) { global $database; if ($catId == 0) return; // no more to display; $sql = "SELECT id, name, parent from dir_category WHERE id = '$catId'"; $database->SetQuery($sql); $rows = $database->loadObjectList(); $titles = array(); foreach ($rows as $row){ $titles = getCatPageTitle($row->parent); $titles[] = $row['name']; break; } return $titles; } $titles = getCatPageTitle(7); echo implode(" - ", $titles); That "should" work correct. Quote Link to comment https://forums.phpfreaks.com/topic/143130-solved-how-to-get-all-parents-in-a-hierarchical-category-structure/#findComment-750648 Share on other sites More sharing options...
RichardRotterdam Posted January 30, 2009 Share Posted January 30, 2009 somehow i doubt $database->loadObjectList() and $database->setQuery() will work unless youre using the joomla db class or what ever class it is Quote Link to comment https://forums.phpfreaks.com/topic/143130-solved-how-to-get-all-parents-in-a-hierarchical-category-structure/#findComment-750663 Share on other sites More sharing options...
premiso Posted January 30, 2009 Share Posted January 30, 2009 somehow i doubt $database->loadObjectList() and $database->setQuery() will work unless youre using the joomla db class or what ever class it is I assume he has it some what working with the DB from this statement: Currently it is only outputting; Scotland - Glasgow If it is not working, yea he will have to put in the right DB information. I do not know what class it is, but given the code he originally posted, the code I posted should work just fine, unless that class does some weird stuff. Quote Link to comment https://forums.phpfreaks.com/topic/143130-solved-how-to-get-all-parents-in-a-hierarchical-category-structure/#findComment-750665 Share on other sites More sharing options...
RichardRotterdam Posted January 30, 2009 Share Posted January 30, 2009 Oops you're right my bad it should work fine. I got to start reading better Quote Link to comment https://forums.phpfreaks.com/topic/143130-solved-how-to-get-all-parents-in-a-hierarchical-category-structure/#findComment-750671 Share on other sites More sharing options...
sallieann Posted January 30, 2009 Author Share Posted January 30, 2009 Phew! Thanks for all your help guys - I've sorted it... This is my solution (probably could be done abit better but it works); function getCatPageTitle($catName, $parentId) { global $database; $sql = "SELECT id, name, parent from jos_dir_cat WHERE id = ". (int) $parentId .""; $database->SetQuery($sql); $rows = $database->loadObjectList(); $titleArray[0] = $catName; foreach ($rows as $row){ $titleArray[] = " - "; $titleArray[] = getCatPageTitle($row->name, $row->parent); $parentId = $row->id; } krsort($titleArray); reset($titleArray); $title = ""; foreach($titleArray as $key => $value){ $title.= $value; } return $title; } Quote Link to comment https://forums.phpfreaks.com/topic/143130-solved-how-to-get-all-parents-in-a-hierarchical-category-structure/#findComment-750813 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.