开发者

Recursive function to generate breadcrumbs

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. Alternative开发者_运维百科s are also welcomed.


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'];
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜