在第n级的php中创建treeview

Hi I want to create a treview at nth level I have this query

<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>

This run till two level. but in this way I need to more and more SQL queries . So please help me how I can create a treview at nth level in simple one or two SQL queries . There will be any simple function if can create then please help with thanks.

First, the "mysql_*" functions are quite deprecated. Use PDO instead.

Next, think about using recusive functions to walk your query result. I believe array_walk_recursive should be of great help here.

Query ALL your stuff from database in a single array with parent informations (array should look something like this: array( $element_id => array( 'parent' => $parent_id, $element))) and then create your array tree using that information.

To convert your tree-array into a list, just use array_walk_recursive with a custom callback.

If I recall correctly, there are native functions that can manage this kind of thing but can't get my hand on it at the moment. I will be back as soon as I find it.