dongzi3434 2019-07-14 03:12
浏览 148
已采纳

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

I have a database table I have to build a UL list from

the table is setup like cat-id parent-id (is the cat-id of whatever the parent is) cat-name cat-image

I can get the root cat easily which has a parent-id of "0" but for the life of me I can't figure out how to do it so the end result is

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;
        }
  • 写回答

2条回答 默认 最新

  • dougu1952 2019-07-14 05:37
    关注

    You can parse a sub category like this, but it does requirer to make a sql connection every time you check what categories does parent has:

    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[$key] = $cat["cat-name"];
                        $sql_subCategory = "SELECT * FROM cats where parent_id=". $cat["id"];
                                $res_sub = mysqli_query($con, $sql_subCategory);
                                if(mysqli_num_rows($res_sub) > 0){
                                    while($cat_sub = $res_sub->fetch_array(MYSQLI_ASSOC)){
                                       $cats[$cat["cat-name"]] = $cat_sub["cat-name"];
                                    }
                                }
                            }
                        }
                    }
                    mysqli_free_result($res_sub);
                    mysqli_free_result($res);
                    mysqli_close($con);
                    return $cats;
                }
    

    That should give you the array:

    array(
        'cat1' => 'Some Category',
        'cat2' => array(
            'sub1' => 'name',
        ),
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?