How to create multilevel drop-down tree list?

The issue is the required drop-down multilevel tree list that retrieves data from the database, have the code, with one exception that is not configured the expansion here is the screenshot of the result:
here is the code
header("Content-Type: text/html; charset=UTF-8");
//Select data from database
$result=mysql_query("SELECT * FROM table1");
//If in the database there is a record generated by an array
if (mysql_num_rows($result) --> 0){
 $cats = array();
//In a loop formed by an array of sections, the key will be the id of the parent category, as well as an array of sections, the key is the id of the category
 while($cat = mysql_fetch_assoc($result)){
 $cats_ID[$cat['uid']][] = $cat;
 $cats[$cat['pid']][$cat['uid']] = $cat;
function build_tree($cats,$parent_id,$only_parent = false){
 if(is_array($cats) and isset($cats[$parent_id])){
 $tree = '<ul>';
 foreach($cats[$parent_id] as $cat){
 $tree .= '<li>'.$cat['name'].' #'.$cat['uid'];
 $tree .= build_tree($cats,$cat['uid']);
 $tree .= '</li>';
 $cat = $cats[$parent_id][$only_parent];
 $tree .= '<li>'.$cat['name'].' #'.$cat['uid'];
 $tree .= build_tree($cats,$cat['uid']);
 $tree .= '</li>';
 $tree .= '</ul>';
 else return null;
 return $tree;
echo build_tree($cats,0);
June 14th 19 at 21:02
1 answer
June 14th 19 at 21:04
Here's a simple example in Jquery
figured out the problem? - peter.Armstro commented on June 14th 19 at 21:07

Find more questions by tags AJAXJavaScriptjQueryMySQLPHP