2014-12-07 51 views
0

嗨创建t检讨PHP我想在第n层 创建我t检讨有这个疑问在第n层

<ul style="list-style:none;" id="MainMenu"> 
        <?php 
        $selectfolders=mysql_query("select * from tbl_folders where user_id=1"); 
        while($foldername=mysql_fetch_array($selectfolders)) 
        {?> 
        <li style="padding:5px;" > 
         <?php echo $foldername['title'];?> 
         <?php 
         $selecttasks=mysql_query("select * from tbl_tasks where project_id=$foldername[f_id] and parent_id=0"); 
         $numtask=mysql_num_rows($selecttasks); 
         if($numtask>=1) 
         { 
         ?> 
         <ul id="TaskList"> 
          <?php 
          while($tasks=mysql_fetch_array($selecttasks)){ 
           if($tasks['parent_id']==0) 
           {?> 
           <li><?php echo $tasks['title'];?></li> 
          <?php } 
           else 
           { 
           ?> 
           <li><?php echo $tasks['title'];?> 

////////////////////Here will be again new query for subtask and for next step again need to more queries/////////// 

     <?php $selectsubtasks=mysql_query("select * from tbl_tasks where parent_id=$tasks[t_id]");?> 

            <ul id="SubTaskList"> 
             <?php while($subtask=mysql_fetch_array($selectsubtasks)) 
             {?> 
             <li><?php echo $subtask['title'];?></li> 
             <?php }?> 

            </ul> 
           </li>  
          <?php } 

          }?> 
         </ul> 
        <?php }?> 
       </li> 
      <?php }?> 
      </ul> 

此运行至二级。但这样我需要越来越多的SQL查询。 所以请帮助我如何在简单的一个或两个SQL查询中创建第n级treview。 会有任何简单的功能,如果可以创建然后请帮助谢谢。

回答

0

首先,“mysql_ *”函数是相当不推荐的。改为使用PDO

接下来,考虑使用recusive函数来遍历查询结果。我相信array_walk_recursive应该在这里有很大的帮助。

从父数据库中查询所有来自数据库的东西(数组应该看起来像这样:array($element_id => array('parent' => $parent_id, $element))),然后使用该信息创建数组树。

要将您的树型数组转换为列表,只需使用array_walk_recursive自定义回调。

如果我记得正确的话,有一些本机功能可以管理这种事情,但目前还无法掌握它。我会尽快找回来的。

+0

无法编辑添加第二个链接,所以我把它张贴在这里:[this](http://php.net/manual/en/class.recursivetreeiterator.php)可能也有帮助。 – Omniarchos 2014-12-08 13:41:53