你好,我有一个愚蠢的问题。但也许不是。在数据库中,我有表格类别,并且有 4 个列 ID、父 ID、标题和描述。Parent_id 是当前类别的孩子。
Id | parent_id | title | description
___________________________________________________
1 | 1 | Main Category | description
2 | 1 | Sub Category | description
3 | 2 | Other Category | description
4 | 1 | Some Category | description
好的,我知道如何输入数据并显示结果;那不是问题。当我在表格中显示结果时
<table>
<tr>
<td> Id </td>
<td> Parent </td>
<td> Title </td>
<td> Desc </td>
</tr>
<?php foreach($categories as $category): ?>
<tr>
<td> <?php echo $category->id; ?> </td>
<td> <?php echo $category->parent_id; ?> </td>
<td> <?php echo $category->title; ?> </td>
<td> <?php echo $category->eescription; ?> </td>
</tr>
<?php endforeach;?>
</table>
这很好用,并在其他父类别下显示子类别:表如下所示:
1 | 1 | Main Category | description
2 | 1 | Sub Category | description
3 | 2 | Other Category | description
4 | 1 | Some Category | description
我的问题是如何像这样列出所有类别和子类别
|-- Main Category
|--- Sub Category
|--- One more cat
|--Other Category
|--- Some Category
像这样
是否可以使用 forach() 获取这些数据?任何示例如何执行此操作。
谢谢