无法使用递归函数PHP显示Breadcrumb

时间:2011-08-25 02:50:35

标签: php function recursion breadcrumbs

如果有人可以在PHP中使用递归函数方法帮助我显示面包屑。

我收到了这段代码:

function getCategoryTreeIDs($qs_type_id) {
        $crumbsql = "SELECT parent_id FROM lists WHERE id=$qs_type_id";
        $crumbresult = tep_db_query($crumbsql);
        $crumbrow = tep_db_fetch_array($crumbresult);
        $path = array();
        if (!$crumbrow['parent_id'] == '') {
            $path[] = $crumbrow['parent_id'];
            $path = array_merge($this->getCategoryTreeIDs($crumbrow['parent_id']), $path);
        }
        return $path;
}
function showCatBreadCrumb($qs_type_id) {
        $array = $this->getCategoryTreeIDs($qs_type_id);
        $numItems = count($array);
        for ($i = 0; $i<=$numItems-1; $i++) {
            echo $this->getNameLink($array[$i]) . ' &raquo; ';
        }
}

但是,当我点击任何链接(类别)时,面包屑没有显示出来。 如果显示面包屑代码有任何错误?

任何帮助都会受到赞赏。我已经在寻找过去几个月的线索了。

非常感谢!

编辑: 显示的代码不使用“for”命令。

   function getCategorytTreeIDs($qs_type_id) {
    global $lists;
    $crumbsql = "SELECT * FROM lists WHERE id=$qs_type_id";
    $crumbresult = mysql_query($crumbsql);
    $crumbrow = mysql_fetch_array($crumbresult);
    if($crumbrow['parent_id'] == 0) {
        $crumbprob = $crumbrow['problem'];
        return "<a href='index.php'>Home</a> > <a href='index.php?q=id/$qs_type_id'>".$crumbprob."</a> > ";
    } else {
        $crumbprob = $crumbrow['problem'];  
        return getCategoryTreeIDs($crumbrow['parent_id']). "<a href='index.php?q=id/$qs_type_id'>".$crumbprob."</a> >";
    }
}

要显示痕迹,我必须手动输入功能和ID号。像这样:

echo getCategoryTreeIDs(20);

我的问题是,当某些用户点击类别ID时,我如何自动显示面包屑?

感谢。

1 个答案:

答案 0 :(得分:0)

要为请求的页面动态显示面包屑,您需要检查q GET参数。

list($qName, $qID) = explode('/', $_GET['q']); // for categories qName will be 
                                               // 'id' and qID will the be id
if($qName == 'id') {
    echo getCategoryTreeIDs($qID); //Outputs breadcrumbs for category pages
}