您将需要这样的结构(我不会详细说明确切的规范,但递归需要什么):
table_categories:
- record_id
- parent_id
- category_name
+-----------+-----------+---------------------+
| record_id | parent_id | category_name |
+-----------+-----------+---------------------+
| 1 | NULL | Parent Category 1 |
| 2 | NULL | Parent Category 2 |
| 3 | 1 | Child Category 1 |
| 4 | 3 | Subchild Category 1 |
| 5 | 2 | Child Category 2 |
+-----------+-----------+---------------------+
设置好数据库表并设置 record_id 和 parent_id 字段后,使用以下代码获取树结构:
// Create a new class to manage structure generation
class treeStructure
{
// Create a property to store the database records
private $structureData;
// This function will retrieve all records from the database
// We can use PHP to manage the database, rather than relying
// on recursive SQL queries
function getRecords() {
// Generate a db connection
try {
$db = new PDO($dsn, $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
}
// Retrieve all the records from the database
$result = $db->prepare("SELECT record_id, parent_id, category_name FROM table_categories");
$result->execute();
// Save the data array to an object property
$this->structureData = $result->fetchAll(PDO::FETCH_ASSOC);
// Return a default true value
return true;
}
// This function will count the number of children for any specified
// parent
function countChildren($parentId = 0){
// Set a default value to return. By default
// Say this element has 0 children
$childCount = 0;
// Loop through each of the results
foreach($this->structureData as $row){
// If the current records parent ID is the same
// as the supplied parent ID, add 1 to the child
// count
if((int)$row['parent_id']===(int)$parentId) {
$childCount += 1;
}
}
// Return the number of children
return $childCount;
}
// This method will generate our HTML tree structure
function generateStructure($parentId = 0) {
// Define a default value for $html
$html = '';
// Loop through the results
foreach($this->structureData as $row){
// If the current records parent ID equals the requested
// parent ID...
if((int)$row['parent_id']==(int)$parentId){
// Add an <li> element
$html .= '<li>' . $row['category_name'];
// Before closing the <li>, check for any children
// If this record does have children, generate a new
// <ul> element, and recall this function with a new
// parent ID
if($this->countChildren($row['record_id']>0)){
$html .= '<ul>';
$html .= $this->generateStructure($row['record_id']);
$html .= '</ul>';
}
// Now close the <li>
$html .= '</li>';
}
}
// Return the generated HTML
return $html;
}
}
$structureObj = new treeStructure();
$structureObj->getRecords();
$html = '<ul>' . $structureObj->generateStructure() . '</ul>';
echo $html;
这是应该发生的事情的基本概述:
- 生成一个新的结构对象
- 从数据库中获取结构的所有记录,并分配给对象属性
- 运行
generateStructure()方法,传递$parentId获取记录
generateStructure()然后遍历所有记录并查找具有parent_id传递给的 id的记录generateStructure()
- 一旦当前类别被添加到结构中,
generateStructure()就会调用方法countChildren()。如果countChildren()返回大于 0 的 int,则当前记录有子记录,所以我们生成另一个菜单元素
generateStructure()然后返回生成的 HTML
上面的代码我没有调试过,可能有一些语法错误。但是,它应该输出类似这样的 html:
<ul>
<li>Parent Category 1
<ul>
<li>Child Category 1
<ul>
<li>Subchild Category 1</li>
</ul>
</li>
</ul>
</li>
<li>Parent Category 2
<ul>
<li>Child Category 2</li>
</ul>
</li>
</ul>