function generateTree($items) { $items = Db::name('tree')->column('id,pid,name'); $tree = array(); foreach ($items as $item) { if (isset($items[$item['pid']])) { $items[$item['pid']]['son'][] = &$items[$item['id']]; } else { $tree[] = &$items[$item['id']]; } } return $tree; } $tree = generateTree(0); function getTreeData($tree) { foreach ($tree as $t) { echo "<li><span><i " . "class=\"icon-minus-sign\"></i>" .$t["name"]. "</span>\n<a href=\"info.php?id=".$t["id"]."\" target=\"_blank\">详细</a><ul>\n"; if (isset($t['son'])) { getTreeData($t['son']); } echo "</ul>\n</li>\n"; } } $list = getTreeData($tree); $this->assign('list',$list); return $this->view->fetch(); }
The static file is like this
is directly output to
and is out of control. If you use variables
Only output one level, and there will be no more after that.
Masters, please teach me how to write, thank you.
You are not out of control, this function echoes, just delete the echo, and the array is returned directly with variables
Splice directly in the view
Don’t write anything on the page, just print the variables transferred from the controller to see what they are