Mike
Mike

Reputation: 1768

How to show subcategories of a selected main category?

I'm trying to build a navigation that when the user selects a category then the navigation will only show sub categories for that category selected.

I'm getting a variable from the URL to pass as the parent id and it looks like so:

locolhost/store.php?c=2

the navigation I'm looking for should look like:

Parent
    child
    child
Parent
Parent
Parent

but, currently my code produces:

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

Here's my current code:

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

I'm just simply calling called getChildren() from buildNav() which gets all the children for the selected category. I think I need a condition that will call getChildren() only when the parent I want to show it's children is going through the loop ... if that makes sense?

Here's my database:

enter image description here

Upvotes: 0

Views: 868

Answers (2)

Mike
Mike

Reputation: 1768

I figured it out ... I needed to add a condition. Here's the working code:

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

Upvotes: 0

Hank
Hank

Reputation: 731

I dont think you are passing the correct 'parent' variable to the child function. Try the following:

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

Upvotes: 1

Related Questions