[英]Recursively Generate Nested Navigation in PHP
I have a php class that looks like this: 我有一个看起来像这样的php类:
class $nav_node {
public $id;
public $page_name;
public $parent_id;
public $page_content;
}
My goal is to build a function that takes a single one of these nav_nodes and shoves it into an existing nested navigation structure. 我的目标是构建一个函数,该函数使用单个nav_nodes中的一个并将其推入现有的嵌套导航结构中。
$nav_nodes = [];
function add_node($new_node) {
}
I want to build the nested navigation structure simply by using html's 我想简单地通过使用html来构建嵌套的导航结构
<ul> and <li> tags ( nested )
This is what the structure would look like when done 这是完成后的结构外观
Thing
thing2
thing3
thing4
thing5
thing6
thing7
Thing8
thing9
Here was my first attempt, but I didn't really see this going anywhere. 这是我的第一次尝试,但我并没有真正看到这一点。
function add_node($new_node) {
global $nav_nodes;
if ( isset($nav_nodes[$new_node->id]) && !is_array($nav_nodes[$new_node->id]) ) {
$nav_node[$new_node->id] = [];
}
$nav_nodes[$new_node->id][] = $new_node;
// Display "new" nested structure
}
I imagine recursion can be used somewhere. 我想可以在某个地方使用递归。 I was thinking that even if this function correctly builds the structure of the array, I still need to somehow display it all.
我在想,即使此函数正确构建了数组的结构,我仍然需要以某种方式显示所有内容。 Maybe that's a separate function?
也许这是一个单独的功能? Maybe that separate display function is the one that is recursive?
也许单独的显示功能是递归的? I think it would be best if this could be a single function.
我认为最好是将其作为一个功能。
Anyways, Thanks for your help!! 无论如何,谢谢您的帮助!
Here is an answer I created which I've adapted from http://www.jugbit.com/php/php-recursive-menu-with-1-query/ which is a really great answer. 这是我根据http://www.jugbit.com/php/php-recursive-menu-with-1-query/改编而成的一个很好的答案。 Very minimal/simple code.
非常少/简单的代码。
class nav_node {
public $id;
public $page_name;
public $parent_id;
public $page_content;
public $items;
public function __construct($id, $page_name, $parent_id, $page_content)
{
$this->id = $id;
$this->page_name = $page_name;
$this->parent_id = $parent_id;
$this->page_content = $page_content;
}
}
class nav {
public function generate($nodes)
{
ob_start();
//echo var_dump(nav::create_array($nodes));
echo '<ul>';
foreach ($nodes as $node)
{
echo '<li>'.$node->page_name.'</li>';
if (count($node->items) > 0)
{
echo nav::generate($node->items);
}
}
echo '</ul>';
return ob_get_clean();
}
public function create_array($nodes, $parent = 0)
{
$out = array();
foreach($nodes as $node){
if($node->parent_id == $parent){
$out[$node->id] = $node;
$out[$node->id]->items = nav::create_array($nodes, $node->id);
}
}
return $out;
}
}
$nodes[] = new nav_node(
1,
'home',
null,
'hello world'
);
$nodes[] = new nav_node(
2,
'about',
null,
'hello world'
);
$nodes[] = new nav_node(
3,
'company',
2,
'hello world'
);
$nodes[] = new nav_node(
4,
'contact',
3,
'hello world'
);
$nodes[] = new nav_node(
6,
'offices',
3,
'hello world'
);
$nodes[] = new nav_node(
7,
'staff',
3,
'hello world'
);
//var_dump($nodes);
nav::generate(nav::create_array($nodes));
Output: 输出:
- home
家
- about
关于
- company
公司
- contact
联系
- offices
办事处
- staff
员工
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.