2010-03-03 22 views
0

我对CodeIgniter和PHP一般都很陌生,有两个问题有点相关。在codeigniter的嵌套树中生成适当的路由

我有一个具有无限深度的嵌套类树,可以生成像myapp.com/language和myapp.com/spanish这样的链接,但我希望拥有类似myapp.com/language/spanish的内容。

仅供参考我的模型返回数组像一个数组:

[Languages] => Array ([category_id] => 4 [sub] => Array ([Japanese] => Array ([category_id] => 5) [Spanish] => Array ([category_id] => 6)... 

我的辅助函数来创建菜单:

function buildMenu($menu_array, $is_sub=FALSE){ 

    /* 
    * If the supplied array is part of a sub-menu, add the 
    * sub-menu class instead of the menu ID for CSS styling 
    */ 
    $attr = (!$is_sub) ? ' id="menu"' : ' class="submenu"'; 
    $menu = "<ul$attr>\n"; // Open the menu container 
    /* 
    * Loop through the array to extract element values 
    */ 
    foreach($menu_array as $id => $properties) { 

     /* 
     * Because each page element is another array, we 
     * need to loop again. This time, we save individual 
     * array elements as variables, using the array key 
     * as the variable name. 
     */ 
     foreach($properties as $key => $val) { 

      /* 
      * If the array element contains another array, 
      * call the buildMenu() function recursively to 
      * build the sub-menu and store it in $sub 
      */ 
      if(is_array($val)) 
      { 
       $sub = buildMenu($val, TRUE); 
      } 

      /* 
      * Otherwise, set $sub to NULL and store the 
      * element's value in a variable 
      */ 
      else 
      { 
       $sub = NULL; 
       $$key = $val; 
      } 
     } 

     /* 
     * If no array element had the key 'url', set the 
     * $url variable equal to the containing element's ID 
     */ 
     if(!isset($url)) { 
      $url = $id; 
     } 

     /* 
     * Use the created variables to output HTML 
     */ 
     $menu .= "<li><a href='$url'>$url</a>$sub</li>\n"; 

     /* 
     * Destroy the variables to ensure they're reset 
     * on each iteration 
     */ 
     unset($url, $display, $sub); 

    } 
    return $menu . "</ul>\n"; 

这使我对我下一个问题。我如何获得这些链接来调用一个动态函数,该函数可以返回该类别中所有帖子的列表。我在想,如果我能够获得上述路线,我会如何让它们像上面那样,它就像拥有像get_cat_posts($cat_id)这样的功能一样简单。我在这个假设中纠正了吗?是否有多个子级别是一个问题?

对不起,很长的文章。谢谢你的帮助。

回答

0

那么我已经放弃试图做这项工作。尝试使用这个当前函数来产生我想要的结果似乎站不住脚,所以我已经转向使用邻接列表并缓存结果。咩。