How to recursively build a <select> with unknown tree depth

后端 未结 8 633
栀梦
栀梦 2020-12-15 01:49

I have a MySQL table with a tree data structure. The fields are _id, name and parentId. When the record hasn\'t a parent, parent

8条回答
  •  别那么骄傲
    2020-12-15 02:19

    Pass a parameter to count the iteration like $pass

    function toUL ($arr, $pass = 0) {
    
        $html = '
      ' . PHP_EOL; foreach ( $arr as $v ) { $html.= '
    • '; $html .= str_repeat("--", $pass); // use the $pass value to create the -- $html .= $v['name'] . '
    • ' . PHP_EOL; if ( array_key_exists('children', $v) ) { $html.= toUL($v['children'], $pass+1); } } $html.= '
    ' . PHP_EOL; return $html; }

提交回复
热议问题