如何显示所选主要类别的子类别?

时间:2012-10-29 04:31:39

标签: php

我正在尝试构建一个导航,当用户选择一个类别时,导航将只显示所选类别的子类别。

我从URL中获取一个变量作为父ID传递,它看起来像这样:

locolhost/store.php?c=2

我正在寻找的导航应该是这样的:

Parent
    child
    child
Parent
Parent
Parent

但是,目前我的代码产生了:

Parent
    child
    child
Parent
    child
    child
Parent
    child
    child
Parent
    child
    child

这是我目前的代码:

shop.php

$parent_id = $_GET['p'];
include('navigation.php');
$navigation = new navigation;
print($navigation->main($parent_id));

navigation.php

public function main($parent)
{
    /* PDO */
    $array = $categoryVIEW->fetchAll(PDO::FETCH_ASSOC);
    return $this->buildNav($array,$parent);
}
private function buildNav($array,$parent)
{
    $html = '';
    foreach($array as $item)
    {
        if($item['parent'] === NULL)
        {
            $html .= "\n<div class=\"parent\"><a href=\"?p=".$item['category_id']."\">".$item['category_name']."</a></div>\n";
            $html .= "<div class=\"child\">\n";
            $html .= $this->getChildren($array,$parent);
            $html .= "</div>\n";
        }
    }
    return $html;
}
private function getChildren($array,$parent)
{
    $html = '';
    foreach($array as $item)
    {
        if($item['parent']===$parent)
        {
            $html .= "\t<a href=\"?p=".$item['category_id']."\">".$item['category_name']."</a>\n";
        }
    }
    return $html;
}

我只是简单地从getChildren()调用名为buildNav()的内容,它会获取所选类别的所有子项。我想我需要一个条件,只有当我想要显示它的父母正在经历循环时才会调用getChildren() ...如果这有意义的话?

这是我的数据库:

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为你没有将正确的'parent'变量传递给子函数。请尝试以下方法:

private function buildNav($array,$parent)
{
    $html = '';
    foreach($array as $item)
    {
        if($item['parent'] === NULL)
        {
            $html .= "\n<div class=\"parent\"><a href=\"?p=".$item['category_id']."\">".$item['category_name']."</a></div>\n";
            $html .= "<div class=\"child\">\n";
            // the following line needs to be changed
            $html .= $this->getChildren($array,$item['category_id']);
            $html .= "</div>\n";
        }
    }
    return $html;
}

答案 1 :(得分:0)

我想通了......我需要添加一个条件。这是工作代码:

private function buildNav($array,$parent)
{
    $html = '';
    foreach($array as $item)
    {
        if($item['parent'] === NULL)
        {
            $html .= "\n<div class=\"parent\"><a href=\"?p=".$item['category_id']."\">".$item['category_name']."</a></div>\n";
            $html .= "<div class=\"child\">\n";

            /* had to add this condition */
            if($item['category_id'] === $parent)
            {
                $html .= $this->getChildren($array,$parent);
            }               
            $html .= "</div>\n";
        }
    }
    return $html;
}
private function getChildren($array,$parent)
{
    $html = '';
    foreach($array as $item)
    {
        if($item['parent'] === $parent)
        {
            $html .= "\t<a href=\"?p=".$item['category_id']."\">".$item['category_name']."</a>\n";
        }           
    }
    return $html;
}