Ekrem Doğan
Ekrem Doğan

Reputation: 694

Printing down all row elements as tree in PHP

I built a database that has a table called "tasks". Table tasks is in charge of keeping tasks in order of their first entry date. Tasks table has a column named "parent". This parent column enables user to create a new task under another task. For example we have a task, its id is 21. When we create a new "child" task under 21th task, we set parent column of new task to 21; so that it implies that new task is a task under 21th task. I included an image of table so that it gets easier to understand what's going on. https://i.sstatic.net/M9cmZ.jpg

What I am trying to achieve with all those tasks is that I want them printed out as tree, a unordered list if you may. Structure of the list is like following:

<ul id="tree">
        <li><a href="#">A root Task</a>
            <ul>
                <li><a href="#">child of first task</a>
                    <ul>
                        <li><a href="#">grandchildren of first child</a></li>
                        <li><a href="#">grandchildren of first child</a></li>
                    </ul>                           
                </li>
                <li><a href="#">child of first task</a></li>
                <li><a href="#">child of first task</a>
                    <ul>
                        <li><a href="#">grandchildren of first child</a></li>
                        <li><a href="#">grandchildren of first child</a></li>
                        <li><a href="#">grandchildren of first child</a></li>
                    </ul>                           
                </li>
                <li><a href="#">child of first task</a>
                    <ul>
                        <li><a href="#">grandchildren of first child</a></li>

                    </ul>                           
                </li>
                <li><a href="#">child of first task</a></li>
            </ul>                   
        </li>
</ul>

I came up with couple of ideas, none of them is working. Is there any ideas for that?

A root task in the example above is a task from tasks table that its parent value is 0. There may be a lot of them, parentless tasks, and there will no limit on creating task under another.

Upvotes: 1

Views: 1041

Answers (1)

Lan
Lan

Reputation: 709

Maybe you have thought of that.. but..

The first idea that comes to my mind is a simple function that returns all the childs that match with the given parent_id..

So, the first call is get_childs(0), and he will return all the Tasks with parent_id='0'.

And each one, will call again the same function, with his parents id on it. If one of the main tasks has id=50, and in the table exists parent_id=50, they will print under his parent.

function get_childs($parent_id){

     if ($result = $mysqli->query("SELECT * FROM table WHERE parent_id='".$parent_id."'")) {
         echo '<ul>';

         while ($row = $result->fetch_assoc()) {
             echo '<li><a href="#">'.$row['name'].'</a>';

             //call again for each child. if the id dosen't have childs, then prints nothing and go on!
             get_childs($row['id']);

             echo '</li>';
        }// end while

        echo '</ul>';
     }// end if select

} // end function

Hope it helps!

Upvotes: 2

Related Questions