0

ユーザーがカテゴリを選択すると、選択したカテゴリのサブカテゴリのみがナビゲーションに表示されるナビゲーションを構築しようとしています。

親IDとして渡す変数をURLから取得しています。次のようになります。

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));

ナビゲーション.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()fromを呼び出すだけです。buildNav()親が子であることを示したい場合にのみ呼び出される条件が必要だと思いgetChildren()ます...それが理にかなっている場合は?

ここに私のデータベースがあります:

ここに画像の説明を入力

4

2 に答える 2

1

正しい「親」変数を子関数に渡しているとは思いません。次のことを試してください。

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;
}
于 2012-10-29T04:58:25.937 に答える
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;
}
于 2012-10-30T00:50:12.503 に答える