skip to Main Content
function showcategory($parentid)
{
     $sql = "select * from categories where parent_id='".$parentid."'";
     $result = mysqli_query($GLOBALS['conn'],$sql);
     $output ="<ul id ='tree1' >n";
    while($data=mysqli_fetch_array($result))
    {
        $output.="<li>n".$data['name'];
        $output.=showcategory($data['id']);
        $output.="</li>";
    }
    $output.="</ul>";
    return $output;
}

I have a treeview created using PHP, and the function that extracts the treeview extracts the category and its subcategory properly. The categories table in the database has a column that holds the specified values (1/0) where 1 is for main categories and 0 is for subcategories. The problem I face is that when I extract the categories from the database, there is no distinction between whether the categories are main or subcategories. They are all displayed as li elements, and I use CSS formatting on the treeview to create a folder icon on the list items. The problem is how to display the main categories that have a value of 1 in the categories table as an ul element that contains a folder icon that can be opened to show what’s inside, and the subcategories that have a value of 0 in the categories table should be displayed as li elements without any folder icon (meaning that only the subcategory name is displayed as text).

my output
enter image description here

I need to get outputs as shown in this image.
enter image description here

2

Answers


  1. I suggest you modify your code so that it takes into consideration whether the item is a main category or subcategory.

    "a column that holds the specified values (1/0) where 1 is for main categories and 0 is for subcategories" – let’s assume this column is called type.

    function showcategory($parentid)
    {
        $sql = "select * from categories where parent_id='".$parentid."'";
        $result = mysqli_query($GLOBALS['conn'],$sql);
        $output ="<ul id ='tree1' >n";
        while($data=mysqli_fetch_array($result))
        {
            $output.= '<li class="type' . $data['type'] . '">'.$data['name'];
            if ($data['type']) {
                $output.=showcategory($data['id']);
            }
            $output.="</li>";
        }
        $output.="</ul>";
        return $output;
    }
    

    You should then be able to style the subcategory nodes using CSS, matching on li.type0

    Login or Signup to reply.
  2. I suggest you to query into database only one time other than put DB query in a recursion function.

    Take you needs to check where a menu item is main (base on type value 0/1). This add class=’main’ to main item and you can use css to style the main icon. Let’s try this modification.

    function showcategory($data, $parentId = 0) {
    $menu = '<ul>';
    
    foreach ($data as $item) {
        if ($item['parent_id'] == $parentId) {
            $menu .= '<li';
            
            // Add 'main' class if 'type' is 1
            if ($item['type'] == 1) {
                $menu .= ' class="main"';
            }
    
            $menu .= '>';
            $menu .= '<a href="#">' . $item['name'] . '</a>';
            
            // Recursive call to build sub-menu if it has children
            $submenu = showcategory($data, $item['id']);
            if ($submenu) {
                $menu .= $submenu;
            }
    
            $menu .= '</li>';
        }
    }
    
    $menu .= '</ul>';
    
    return $menu;
    }
    
    $sql = "select * from categories where parent_id='".$parentid."'";
    $result = mysqli_query($GLOBALS['conn'],$sql);
    $data=mysqli_fetch_array($result)
    
    $menu = showcategory($data);
    echo $menu;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search