Print hierachical data in a parent child form unordered list php?

前端 未结 2 1440
执笔经年
执笔经年 2021-01-16 14:06

I have data in mysql table in a parent child hierarchy like;

|---------+-----------+-------------|
| msg_id  | parent_id |    msg      |
|---------+---------         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-16 14:38

    function get_list($parent='NULL', $counter=0, $spaces=""){
    
        $sql = "SELECT * FROM t1 WHERE parent_id = ".parent;
        $rs[$counter] = mysql_query($sql) or die(mysql_error());
        while($row[$counter] = mysql_fetch_array($rs[$counter])){
            echo $spaces.$row[$counter]['msg']."
    "; get_list($row[$counter]['parent_id'], $counter+1, "  ".$spaces); } mysql_free_result($rs[$counter]); }

    Or, close to that.

提交回复
热议问题