tags:

views:

143

answers:

2
function createPath($id, $category_tbl, $path) {

    $s = "SELECT * FROM ".$category_tbl." WHERE ID = $id";
    $r = mysql_query($s);
    $row = mysql_fetch_array($r);

    if($row['PARENT_ID'] == 0) {
        $path .=$row['TITLE'].'-';
    }
    else {
        $path .='-'.$row['TITLE'];
        createPath($row['PARENT_ID'],$category_tbl, $path);

    }
    return $path;
}

It is a recursive function that must generate breadcrumbs. I cannot get it to work properly, it only returns the last TITLE.

the sql table is something like ID, TITLE, PARENT_ID a PARENT_ID = 0 means the category has no parent, for any other PARENT_ID, go to that ID, get it's title and add it to the $path variable

I need help to make this work. Alternatives are also welcomed.

A: 

Looks like you either need to use the value returned by createPath or have $path passed by reference, &$path. One or the other, but not part of each.

Don
+2  A: 

Try something like this:

function createPath($id, $category_tbl) {

    $s = "SELECT * FROM ".$category_tbl." WHERE ID = $id";
    $r = mysql_query($s);
    $row = mysql_fetch_array($r);

    if($row['PARENT_ID'] == 0) {
        return $row['TITLE'];
    } else {
        return createPath($row['PARENT_ID'],$category_tbl).'-'.$row['TITLE'];
    }
}
Messa
thank you, works like a charm