循环通过标题选项卡树MySQL和PHP

时间:2019-02-21 07:29:15

标签: php mysql tree tabs iteration

试图遍历从mysql表中提取的树以形成井井有条的标题选项卡,而我这样做是因为在每个选项卡上我都可以授予特定用户查看或不查看该选项卡的权限。 该表包括选项卡的详细信息 表格看起来像这样:

表格标签

Id        Text         isparent        parentid        hassub
1         Menu 1          1                              1
2         Menu 2          1                              1
150       Menu Level 1                     1             1
151       Menu Level 1.0                   150           1
152       Menu Level 1.00                  151           1
153       Menu Level 1.000                 152           0
154       Menu Level 2                     1             1
157       Menu Level 2.x                   154           0
158       Menu Level 1.x                   150           0
159       Menu Level 1.y                   150           1
160       Menu 2.1                         2             1
161       Menu 2.11                        160           0
163       Menu Level 1.yy                  159           0



 echo '<ul id="main-menu" class="main-menu">';


                $selectparent = "SELECT * FROM tabs WHERE isparent>0";
                $runparent=mysql_query($selectparent,$con);
                if(!$runparent)die("ERROR11");

                for($counterparent=0;$rowparent=mysql_fetch_assoc($runparent);$counterparent++){

                    $parentid=$rowparent['Id'];
                    $parenttext=$rowparent['text'];
                    $parenthassub=$rowparent['hassub'];

                    if($parenthassub==1){       // if parent has sub , select from child tab where parent id =

                        echo '<li class="has-sub">';
                        echo 'text';

                        while($parentid>0){

                            $selectchildparent = "SELECT * FROM tabs 
                                            WHERE parentid='$parentid'";
                            $runchildparent=mysql_query($selectchildparent,$con);
                            if(!$runchildparent)die("ERROR11");
                            echo "<ul>";
                            for($counterchildparent=0;$rowchildparent=mysql_fetch_assoc($runchildparent);$counterchildparent++){

                                $childid=$rowchildparent['Id'];
                                $childhassub=$rowchildparent['hassub'];
                                $childtext=$rowchildparent['text'];

                                if($childhassub==1){    // if child has sub select from childtab where child_id=childid
                                    echo '<li class="has-sub">';
                                    echo 'text';
                                    $parentid=$childid;
                                }
                                else{
                                    echo '<li>';
                                    echo 'text';
                                    echo '</li>';

                                }
                            }
                            $parentid=0;
                            echo "</ul>";
                        }
                    }
                    else{
                                    echo '<li>';
                                    echo 'text';
                                    echo '</li>';
                    }
                }
                echo '</ul>';

如果该标签具有子标签,那么我需要将其作为标头标签(基于树),以读取具有相同父ID的其他子标签。

1 个答案:

答案 0 :(得分:1)

使用mysqli语句,因为不推荐使用mysql语句。 试试这个,

$con = mysqli_connect($servername, $username, $password, $db);

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

echo '<ul id="main-menu" class="main-menu">';
$selectparent = "SELECT * FROM tabs WHERE isparent>0";
$runparent= mysqli_query($con, $selectparent);

$rowparent = mysqli_fetch_all($runparent,MYSQLI_ASSOC);
for($counterparent=0;$counterparent < count($rowparent);$counterparent++){

    $parentid=$rowparent[$counterparent]['Id'];
    $parenttext=$rowparent[$counterparent]['text'];
    $parenthassub=$rowparent[$counterparent]['hassub'];
    echo '<h4>'.$parenttext.'</h4>';
    recursive_loop($con,$parentid,$parenthassub);
}
echo '</ul>';

function recursive_loop($con, $id, $hassub){
    if($hassub == 0)
        return;
    echo '<ul>';
    $selectparent = "SELECT * FROM tabs WHERE parentid = $id";
    $runparent= mysqli_query($con, $selectparent);
    if($runparent){

        $rowparent = mysqli_fetch_all($runparent,MYSQLI_ASSOC);

        for($counterparent=0;$counterparent < count($rowparent);$counterparent++){

            $parentid=$rowparent[$counterparent]['Id'];
            $parenttext=$rowparent[$counterparent]['text'];
            $parenthassub=$rowparent[$counterparent]['hassub'];
            echo '<li class="has-sub">';
            echo $parenttext;
            echo'</li>';
            recursive_loop($con,$parentid,$parenthassub);
        }
    }
    echo '</ul>';
}

我使用了递归,因为我们不知道您的菜单有多深。