Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This is my database table

| id| category| parent_id|
|:---- |:------:| -----:|
| 1| category1|  |
| 2| category2|  |
| 3| category3|  |
| 4| subcategory4| 2 |
| 5| subcategory5| 1 |
| 6| subcategory6| 3 |
| 7| subcategory7| 1 |



My expected output should look like this:

| category| Parent Category|
|:---- |:------:|
| category1| |
| category2| |
| category3| |
| subcategory5| category1|
| subcategory7| category1|
| subcategory4| category2|
| subcategory6| category3|

I have been working on this for a while now.. Can anyone please tell me how can I modify my code or use any other method to achieve my expected output?


What I have tried:

This is my code
   

    $result = mysqli_query($con,"SELECT * FROM category_table ORDER BY parent_id");
        
        $category = array(
        'categories' => array(),
        'parent_cats' => array()
        );
        
        //build the array lists with data from the category table
        while ($row = mysqli_fetch_assoc($result)) {
        //creates entry into categories array with current category id ie. $categories['categories'][1]
        $category['categories'][$row['id']] = $row;
        //creates entry into parent_cats array. parent_cats array contains a list of all categories with children
        $category['parent_cats'][$row['parent_id']][] = $row['id'];
                                                                    }
        
        function buildCategory($parent, $category) {
        $html = "";
        if (isset($category['parent_cats'][$parent])) {
            
            foreach ($category['parent_cats'][$parent] as $cat_id) {
                if (!isset($category['parent_cats'][$cat_id])) {
                    $html .= "<tr>\n";
                    $html .= "<td>\n  <a href=''>" . $category['categories'][$cat_id]['category'] . "</a>\n</td> \n";
                    $html .= "</tr>\n";
                }
                if (isset($category['parent_cats'][$cat_id])) {
                    $html .= "<tr>\n";
                    $html .= "<td>\n  <a href=''>" . $category['categories'][$cat_id]['category'] . "</a> \n";
                    $html .= buildCategory($cat_id, $category);
                    $html .= "</td> \n";
                    $html .= "</tr>\n";
                }
            }
            
        }
        return $html;
        }
        
        
        echo buildCategory('', $category);?>


Output for above code looks like:

| category1|
|:---- |
| subcategory5|
| subcategory7|

| category2|
|:---- |
| subcategory4|

| category3|
|:---- |
| subcategory6|
Posted
Comments
[no name] 10-Dec-21 15:45pm    
You do a "self-join" (parent id not null) and a "union" (parent id is null) with result of self-join.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900