如何显示多级一对多关系树

How to loop all records and display all the respective children using HTML <ul></ul>? I tried using PHP Do While but stuck at only 1 level.

MySQL (select * from user)

enter image description here

Desired output

Tree View

enter image description here

List View

enter image description here

The easy was is to do that with the help of array. Hope it helps.

$data = array();
foreach ($result as $item) {
    $key = $item['name']; // or $item['info_id']
    if (!isset($data[$key])) {
        $data[$key] = array();
    }

    $data[$key][] = $item;
}

You can use this code:

$aResults; // it is your mysql result (array)

$resultSorted = array();
$resultSorted = recursiveList($aResults, '');

function recursiveList(&$aResults, $iKey)
{
    $aChilds = '<ul>';
    foreach ($aResults as $iLoopKey => $aResult) {
        if ($aResult['parent'] == $iKey) {
            unset($aResults[$iLoopKey]);
            $aChilds .= '<li>' . $aResult['name'] . '</li>';
            $aChilds .= recursiveList($aResults, $aResult['name']);
        }
    }
    return $aChilds . '</ul>';
}

// Output example
echo '<pre>';
print_r($resultSorted);

As a result, I recived:

enter image description here

Also you'd better use 'parent_id' instead 'parent' in yours tables.