Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php嵌套集,在导航菜单中构建链接_Php_Mysql_Codeigniter_Navigation_Nested Sets - Fatal编程技术网

php嵌套集,在导航菜单中构建链接

php嵌套集,在导航菜单中构建链接,php,mysql,codeigniter,navigation,nested-sets,Php,Mysql,Codeigniter,Navigation,Nested Sets,我的数据库中有几个类别,我正在检索类似这样的内容(括号中的数字表示深度): 新的(1) -一般(2) -控制台(2) 游戏(1) -pc(2) --仿真器(3) -ps3(2) 然后,我有一个函数来处理多维数组并生成导航菜单 --模型 这将从数据库返回以下数组 Array ( [0] => Array ( [name] => News [depth] => 1 ) [1] => Array ( [name] => General [depth] =

我的数据库中有几个类别,我正在检索类似这样的内容(括号中的数字表示深度):

新的(1)
-一般(2)
-控制台(2)

游戏(1)
-pc(2)
--仿真器(3)
-ps3(2)

然后,我有一个函数来处理多维数组并生成导航菜单

--模型
这将从数据库返回以下数组

Array (
    [0] => Array ( [name] => News [depth] => 1 ) 
    [1] => Array ( [name] => General [depth] => 2 ) 
    [2] => Array ( [name] => Console [depth] => 2 ) 
    [4] => Array ( [name] => Games [depth] => 1 ) 
    [5] => Array ( [name] => PC [depth] => 2 ) 
    [6] => Array ( [name] => emulator [depth] => 3 ) 
    [8] => Array ( [name] => ps3 [depth] => 2 ) 
) 
--控制器

列表是以正确的缩进顺序创建的,但是我遇到的问题是为每个项目生成链接 例如,emulator的链接应该是mysite.com/games/pc/emulator


我将如何实现这一点,感谢您的帮助?

我将采用简单的方法,为每个级别构建一个缓存级别数组和以前计算的路径。当再次看到
1
的“基本级别”时,请清除缓存,以免出现任何无效条目:

此示例代码应适用于您当前拥有的内容(只需将此代码放在当前布局呈现代码的上方,并使用
$path
作为URL):

它所做的是,如果当前路径的
深度
大于1(这意味着存在父级深度),它将采用父级的计算路径(父级的路径应该是完整路径,而不仅仅是父级的名称),然后将当前名称添加到该路径中-这将为您提供完整的嵌套路径。然后,它将生成的路径存储到
$depth
数组中(用当前深度索引),供任何子级使用

在示例中,您指定希望
Emulator
的路径为
mysite.com/games/pc/Emulator
,重点放在小写和域名上。由于我们在路径中使用了
$node['name']
,因此您需要使用
strtolower()

我建议在
的实际行中添加域名;

也许您可以提供更多关于如何在树中递归的详细信息,请发布一些代码:)非常好,效果非常好。非常感谢您的意见。
public function index()
{
$navTree = $this->getNavTree(); //gets array from model
$createNavTree = $this->_renderTree($navTree); //pass array to function
$this->load->view('testnavigation.php', $createNavTree);
}


function _renderTree($tree){    
    $current_depth = 0;
    $counter = 0;

    $result = '';

    foreach($tree as $node){
        $node_depth = $node['depth'];
        $node_name = $node['name'];
        $node_id = $node['categoryid'];

        if($node_depth == $current_depth){
            if($counter > 0) $result .= '</li>';            
        }
        elseif($node_depth > $current_depth){

            $result .= $counter == 0 ? '<ul id="nav">' : '<ul>';
            $current_depth = $current_depth + ($node_depth - $current_depth);
        }
        elseif($node_depth < $current_depth){
            $result .= str_repeat('</li></ul>',$current_depth - $node_depth).'</li>';
            $current_depth = $current_depth - ($current_depth - $node_depth);
        }
        $result .= '<li><a href="#">'.$node_name.'</a>';
        ++$counter;
    }
    $result .= str_repeat('</li></ul>',$node_depth).'</li>';
    $result .= '</ul>';     
    return $result;
}
echo $createNavTree;
function _renderTree($tree) {
    $depths = array();
    foreach ($tree as $node) {
        // build the current path
        $path = (($node['depth'] > 1) ? $depths[$node['depth'] - 1] : '') . $node['name'];

        // set the current path as the current depth's path (to be used for any deeper-nodes)
        $depths[$node['depth']] = $path . '/';

        ... layout rendering code ...
    }
}
$path = (($node['depth'] > 1) ? $depths[$node['depth'] - 1] : '') . strtolower($node['name']);
$result .= '<li><a href="http://mysite.com/' . $path . '">'.$node_name.'</a>';