猿问

递归数据库查询以构建 ul 列表

我有一个数据库表,我必须从中构建一个 UL 列表


该表的设置类似于 cat-id parent-id(是任何父级的 cat-id) cat-name cat-image


我可以轻松获得父级 ID 为“0”的根猫,但在我的一生中,我不知道该怎么做,因此最终结果是


rootcat --childParent ---child


function cats(){

        $sql = "SELECT * FROM `cats";

        $cats;

        $con = mysqli_connect($this->vars["host"], $this->vars["user"], $this->vars["pass"], $this->vars["db"]);

        if(mysqli_connect_errno()){

            print "START MYSQLI ERROR<br/>".

            mysqli_connect_error() .

            "<br/>ENDMYSQLI ERROR";

        }else{

            $res = mysqli_query($con, $sql);

            if(mysqli_num_rows($res) > 0){

                while($cat = $res->fetch_array(MYSQLI_ASSOC)){

                    if($cat["parent-id"] == "0"){

                        $cats[] = $cat["cat-name"];

                    }

                }

            }

            mysqli_free_result($res);

            mysqli_close($con);

            return $cats;

        }


泛舟湖上清波郎朗
浏览 143回答 2
2回答

GCT1015

您可以像这样解析子类别,但是每次检查父类别具有哪些类别时,它确实需要进行 sql 连接:function cats(){&nbsp; &nbsp; $sql = "SELECT * FROM `cats";&nbsp; &nbsp; $cats;&nbsp; &nbsp; $con = mysqli_connect($this->vars["host"], $this->vars["user"], $this->vars["pass"], $this->vars["db"]);&nbsp; &nbsp; if(mysqli_connect_errno()){&nbsp; &nbsp; &nbsp; &nbsp; print "START MYSQLI ERROR<br/>".&nbsp; &nbsp; &nbsp; &nbsp; mysqli_connect_error() .&nbsp; &nbsp; &nbsp; &nbsp; "<br/>ENDMYSQLI ERROR";&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; $res = mysqli_query($con, $sql);&nbsp; &nbsp; &nbsp; &nbsp; if(mysqli_num_rows($res) > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($cat = $res->fetch_array(MYSQLI_ASSOC)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($cat["parent-id"] == "0"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cats[$key] = $cat["cat-name"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql_subCategory = "SELECT * FROM cats where parent_id=". $cat["id"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res_sub = mysqli_query($con, $sql_subCategory);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(mysqli_num_rows($res_sub) > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($cat_sub = $res_sub->fetch_array(MYSQLI_ASSOC)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$cats[$cat["cat-name"]] = $cat_sub["cat-name"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysqli_free_result($res_sub);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysqli_free_result($res);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysqli_close($con);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $cats;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }那应该给你数组:array(&nbsp; &nbsp; 'cat1' => 'Some Category',&nbsp; &nbsp; 'cat2' => array(&nbsp; &nbsp; &nbsp; &nbsp; 'sub1' => 'name',&nbsp; &nbsp; ),);
随时随地看视频慕课网APP
我要回答