使用 php 在单个 mysql 数据库 table 的 table 中显示子类别及其父类别

Display subcategory and its parent category in a table from single mysql database table using php

这是我的数据库table

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

这是我的代码

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

以上代码的输出如下:

category1
subcategory5
subcategory7
category2
subcategory4
category3
subcategory6

我的预期输出应该是这样的:

category Parent Category
category1
category2
category3
subcategory5 category1
subcategory7 category1
subcategory4 category2
subcategory6 category3

我已经为此工作了一段时间。谁能告诉我如何修改我的代码或使用任何其他方法来实现我的预期输出?

试试这个查询,而不是内部联接只会给你所需的数据,
然后您可以根据需要调整数据。

SELECT sub_category.id as s_id, sub_category.category as s_cat_name, p_category.category as p_cat_name 
FROM category_table sub_category 
INNER JOIN category_table p_category ON p_category.id = sub_category.parent_id 
ORDER BY sub_category.id

关于如何在代码中使用它的更新, 试试这个,

$result = mysqli_query($con,"SELECT sub_category.id as s_id, sub_category.category as s_cat_name, p_category.category as p_cat_name FROM category_table sub_category INNER JOIN category_table p_category ON p_category.id = sub_category.parent_id ORDER BY sub_category.id");
    
$html = "<tr><th>Category Id</th><th>Category Name</th><th>Parent Category</th></tr>";

while ($row = mysqli_fetch_assoc($result)) {

            $html .= "<tr>";
            $html .= "<td>".$row['s_id']."</td>";
            $html .= "<td>".$row['s_cat_name']."</td>";
            $html .= "<td>".$row['p_cat_name ']."</td>";
            $html .= "</tr>";          
}
    
echo $html;