Jump to content

[SOLVED] How to get all parents in a hierarchical category structure?


sallieann

Recommended Posts

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!

 

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.

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.

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.

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.

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;

}

 

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.