In MySQL my table appears like this:
In MySQL my table appears like this:
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');
My array
My array
$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())
);
The array output
The array output
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
(
)
)
How do I make it width PHP ?
How do I make it width 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>
This is does not work correctly
This is does not work correctly
<?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";
?>
Thanks works now for me thats functions... http://ideone.com/u2GNV
But i think not valid ul and li tags...
http://ideone.com/u2GNV results for me
<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>
i think valid w3 code
<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>