在MySQL中,我的表格如下所示:
http://sqlfiddle.com/#!2/76717
CREATE TABLE IF NOT EXISTS `tbl_category` (
`id` int(6) NOT NULL auto_increment,
`parent_id` int(4) NOT NULL default '0',
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `tbl_category` (`id`, `parent_id`, `name`) VALUES
(1, 0, 'Category 1'),
(2, 0, 'Category 2'),
(3, 0, 'Category 3'),
(4, 1, 'Category 1. 1'),
(5, 1, 'Category 1. 2'),
(6, 1, 'Category 1. 3'),
(7, 4, 'Category 1. 1. 1'),
(8, 4, 'Category 1. 1. 2'),
(9, 4, 'Category 1. 1. 3');
我的阵列
$arrCategories = array(
array("id" => 1, "parent_id" => 0, "name" => "Category 1", "children" => array()
array("id" => 4, "parent_id" => 1, "name" => "Category 1. 1", "children" => array()
array("id" => 7, "parent_id" => 4, "name" => "Category 1. 1. 1", "children" => array()),
array("id" => 8, "parent_id" => 4, "name" => "Category 1. 1. 2", "children" => array()),
array("id" => 9, "parent_id" => 4, "name" => "Category 1. 1. 3", "children" => array())
),
array("id" => 5, "parent_id" => 1, "name" => "Category 1. 2", "children" => array()),
array("id" => 6, "parent_id" => 1, "name" => "Category 1. 3", "children" => array())
),
array("id" => 2, "parent_id" => 0, "name" => "Category 2", "children" => array()),
array("id" => 3, "parent_id" => 0, "name" => "Category 3", "children" => array())
);
数组输出
Array
(
[id] => 1
[parent_id] => 0
[name] => Category 1
[children] => Array
(
[4] => Array
(
[id] => 4
[parent_id] => 1
[name] => Category 1. 1
[children] => Array
(
[7] => Array
(
[id] => 7
[parent_id] => 4
[name] => Category 1. 1. 1
[children] => Array
(
)
)
[8] => Array
(
[id] => 8
[parent_id] => 4
[name] => Category 1. 1. 2
[children] => Array
(
)
)
[9] => Array
(
[id] => 9
[parent_id] => 4
[name] => Category 1. 1. 3
[children] => Array
(
)
)
)
)
[5] => Array
(
[id] => 5
[parent_id] => 1
[name] => Category 1. 2
[children] => Array
(
)
)
[6] => Array
(
[id] => 6
[parent_id] => 1
[name] => Category 1. 3
[children] => Array
(
)
)
)
)
Array
(
[id] => 2
[parent_id] => 0
[name] => Category 2
[children] => Array
(
)
)
Array
(
[id] => 3
[parent_id] => 0
[name] => Category 3
[children] => Array
(
)
)
如何使它宽度为PHP?
<ul>
<li>Category 1
<ul>
<li>Category 1. 1
<ul>
<li>Category 1. 1. 1</li>
<li>Category 1. 1. 2</li>
<li>Category 1. 1. 3</li>
</ul>
</li>
<li>Category 1. 2</li>
<li>Category 1. 3</li>
</ul>
</li>
<li>Category 2</li>
<li>Category 3</li>
</ul>
这是行不通的
<?php
echo " <ul>\n";
foreach ($categories as $category) {
echo " <li>".$category['ad'];
if ($category['children']) {
echo " <ul>\n";
foreach ($category['children'] as $child) {
echo " <li>".$child['ad']."</li>\n";
}
echo " </ul>\n";
}
echo " </li>\n";
}
echo " </ul>\n";
?>
感谢现在为我工作的功能...... http://ideone.com/u2GNV
但我认为不是有效的ul和li标签......
<ul>
<li>Category 1
<ul>
<li>Category 1. 1
<ul>
<li>Category 1. 1. 1</li>
</ul>
<ul>
<li>Category 1. 1. 2</li>
</ul>
<ul>
<li>Category 1. 1. 3</li>
</ul>
</li>
</ul>
<ul>
<li>Category 1. 2</li>
</ul>
<ul>
<li>Category 1. 3</li>
</ul>
</li>
</ul>
<ul>
<li>Category 2</li>
</ul>
<ul>
<li>Category 3</li>
</ul>
我认为有效的w3代码
<ul>
<li>Category 1
<ul>
<li>Category 1. 1
<ul>
<li>Category 1. 1. 1</li>
<li>Category 1. 1. 2</li>
<li>Category 1. 1. 3</li>
</ul>
</li>
<li>Category 1. 2</li>
<li>Category 1. 3</li>
</ul>
</li>
<li>Category 2</li>
<li>Category 3</li>
</ul>
答案 0 :(得分:5)
您可以创建一个函数来将所有数组解析为menu like
块并使用递归:
表示该阵列:
function printMenu($array){
foreach($array as $item){
if(is_array($item) && isset($item['name'])){
echo "<ul>\n";
if(is_array($item['children'])){
echo "<li>".$item['name'];
printMenu($item);
echo "</li>\n";
} else {
echo "<li>".$item['name']."</li>\n";
}
echo "</ul>\n\n";
}
}
}
来自mysql资源(基于你的表):
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
function printMenu($array){
foreach($array as $item){
if(is_array($item) && isset($item['name'])){
echo "<ul>\n";
if(getChildren($item['id'], false)){
echo "<li>".$item['name'];
printMenu(getChildren($item['id'], true));
echo "</li>\n";
} else {
echo "<li>".$item['name']."</li>\n";
}
echo "</ul>\n\n";
}
}
}
function getChildren($id, $return = false){
global $db;
$stmt = $db->prepare("SELECT * FROM `tbl_category` WHERE `parent_id` = :parent_id");
$stmt->execute(array(':parent_id' => $id));
$num = $stmt->rowCount();
if($num){
if($return){
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $row;
}
return true;
}
return false;
}
$query = "SELECT * FROM `tbl_category` WHERE `parent_id` = 0";
$stmt = $db->query($query);
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
printMenu($row);
和结果: