Display hierarchical data

后端 未结 2 1326
南旧
南旧 2021-01-15 19:05

I am playing around with a code example i found here about \'tree menu\' and wanted to make this question.

function tree($id)
{
$query = \"SELECT `name`,`id`         


        
2条回答
  •  一整个雨季
    2021-01-15 19:28

    You need your tree function to track the level, and then apply padding if the level is greater than 1.

    function tree($id, $level=1) {
        $query = "SELECT `name`,`id` from `table` WHERE `id_parrent` = '$id'";
        $result = mysql_query($query);
        if (mysql_num_rows($result) != 0) {
         echo "
      "; while ($row = mysql_fetch_array($result)) { if ($level == 1) echo "
    • " . $row[name] . "
    • "; else echo "
    • " . $row[name] . "
    • "; tree($row[id], $level + 1); } echo "
    "; } }

提交回复
热议问题