【问题标题】:Display subcategory and its parent category in a table from single mysql database table using php使用php在单个mysql数据库表中的表中显示子类别及其父类别
【发布时间】:2022-01-14 06:12:41
【问题描述】:

这是我的数据库表

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

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

【问题讨论】:

    标签: php html mysql database pdo


    【解决方案1】:

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

    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;
    

    【讨论】:

    • 谢谢.. 我得到了 mysql 查询.. 但我不知道如何修改我的 php 代码来处理这个查询
    • @user_59910105 你还在寻找解决方案吗,如果是,我可以更新我的答案,这可能会对你有所帮助
    • 是的,我仍在寻找解决方案
    • 检查更新的代码,让我知道它是否有效
    猜你喜欢
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多