dongying6896 2014-12-03 05:34
浏览 11
已采纳

在php中创建Unlimited Unordered列表

Im trying to create tree form mysql generated data.

This question has been asked before, but actually it was about creating arrays. So Arrays are creating fine. i am using answer from this method.

http://stackoverflow.com/questions/8587341/recursive-function-to-generate-multidimensional-array-from-database-result

This is the array i am getting.

Array
(
    [0] => Array
        (
            [employee_id] => 1
            [organization_id] => 1
            [parent_organization_id] => 0
            [full_name] => Mohammad Salim Khan
            [employee_order] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [employee_id] => 2
                            [organization_id] => 2
                            [parent_organization_id] => 1
                            [full_name] => Ali Khan
                            [employee_order] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [employee_id] => 5
                                            [organization_id] => 4
                                            [parent_organization_id] => 2
                                            [full_name] => Nadeem Khan
                                            [employee_order] => 3
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [employee_id] => 7
                                                            [organization_id] => 5
                                                            [parent_organization_id] => 4
                                                            [full_name] => Sajjad Ali
                                                            [employee_order] => 4
                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [employee_id] => 3
                            [organization_id] => 3
                            [parent_organization_id] => 1
                            [full_name] => Kamran Durrani
                            [employee_order] => 2
                        )

                )

        )

)

but i am stucked in now putting this data in <ul> ,li list.

Please any ideas how to put this in unordered list so that i get list like this.

<ul>
<li>Mohammad Salim Khan</li>
<ul>
//1
<li>Ali Khan</li>
<ul>
<li>Nadeem Khan</li>
<ul><li>Sajjad Ali</li></ul>
//2
<li>Kamran Durrani</li>
</ul>
</ul>

Update:

  • Muhammad Salim Khan
    • Ali Khan
      • Nadeem Khan
        • Sajjad Ali
    • Kamran Durrani

Its getting very confusing as there are unlimited possibilited of li's..

  • 写回答

1条回答 默认 最新

  • doumou3883 2014-12-03 06:13
    关注

    This is a situation for recursion (a function calling itself). Starting with the list in $myLIst:

    function showList ($myList)
    {
        echo "<ul>";
        foreach ($myList as $row)
        {
            echo ("<li>{$row ['full_name']}</li>
    ");
            if  (isset ($row ['children']))
                showList ($row ['children']);
        }
        echo "</ul>
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化