在PHP中将所有行元素打印为树 [英] Printing down all row elements as tree in PHP
问题描述
我建立了一个数据库,该数据库具有一个称为任务的表。表格任务负责按任务的第一个输入日期顺序对其进行保存。任务表具有名为父的列。该父列使用户可以在另一个任务下创建新任务。例如,我们有一个任务,其ID为21。当在第21个任务下创建一个新的子任务时,将新任务的父列设置为21;将第一个任务的父列设置为21。因此它意味着新任务是第21个任务下的任务。我附上一张桌子的图片,以便您更容易了解发生了什么。
http://i.stack.imgur.com/M9cmZ.jpg
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. http://i.stack.imgur.com/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?
上面示例中的根任务是任务表中的任务,其父值为0。其中可能有很多,无父任务,并且在另一个任务下创建任务没有限制。
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.
推荐答案
也许您已经想到了这一点..但是..
Maybe you have thought of that.. but..
我想到的第一个主意是一个简单的函数,该函数将返回与给定parent_id匹配的所有子代。.
The first idea that comes to my mind is a simple function that returns all the childs that match with the given parent_id..
因此,第一个调用是 get_childs(0)
,他将以 parent_id ='0'
返回所有任务。
So, the first call is get_childs(0)
, and he will return all the Tasks with parent_id='0'
.
然后,每个函数都会再次调用相同的函数,并带有其父母的id。
如果主要任务之一的 id = 50
并且表中存在 parent_id = 50
,
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
希望有帮助!
这篇关于在PHP中将所有行元素打印为树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!