0

I'm trying to create tree from MySQL generated data.

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

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 stuck in putting this data in <ul> / <li> list.

How can I 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

It's getting very confusing as there are unlimited possibilities of li's.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sizzling Code
  • 5,932
  • 18
  • 81
  • 138
  • Given an array of n.n.n.n... (eg 1, 1.1, 1.2, 1.2.1 etc), what format are you looking for the output to be in? – Mike Dec 03 '14 at 05:40
  • @Mike Sir, i am looking in
    • format, i have given example in end of my question. i tried many ways but none of it is working :(
    – Sizzling Code Dec 03 '14 at 05:42
  • In your example, Mohammad is stand-alone while the rest are embedded. Is that a typo? – Mike Dec 03 '14 at 05:49
  • @Mike No, Actually Muhammad Parent ID is 0 `[parent_organization_id] => 0`, There are no others with parent id 0 thats why its standing alone, but its possible that there can be others with parent id 0. – Sizzling Code Dec 03 '14 at 05:54
  • @Mike Updated my question add nested list for clarification. – Sizzling Code Dec 03 '14 at 05:59

1 Answers1

1

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>\n");
        if  (isset ($row ['children']))
            showList ($row ['children']);
    }
    echo "</ul>\n";
}
Mike
  • 2,721
  • 1
  • 15
  • 20