选择查询并在PHP中创建infinate循环

选择查询并在PHP中创建infinate循环,php,Php,我试图在PHP中创建多个循环来选择父项和子项。我有以下代码: <?php $counter = 0; $sql="SELECT * from shop_categories where parent = '' order by name ASC "; $rs=mysql_query($sql,$conn); while($result = mysql_fetch_array($rs)) { $flag = 1; $current_parent_sequence = $res

我试图在PHP中创建多个循环来选择父项和子项。我有以下代码:

<?php
$counter = 0;
$sql="SELECT * from shop_categories where parent = '' order by name ASC ";
$rs=mysql_query($sql,$conn);
while($result = mysql_fetch_array($rs)) {
    $flag = 1;
    $current_parent_sequence = $result["sequence"];

    echo '<strong>'.$result["name"].'</strong><br>';

    while($flag == 1) {
        $counter++;

        $sql2="SELECT * from shop_categories where parent = '".$current_parent_sequence."' order by name ASC ";
        $rs2=mysql_query($sql2,$conn);
        if(mysql_num_rows($rs2) > 0) {
            while($result2=mysql_fetch_array($rs2)) {
                $current_parent_sequence = $result2["sequence"];

                echo $counter.' - '.$result2["name"].'<br>';
            }
        } else {
            $flag = 0;
        }
    }
}
?>

请尝试下面的代码。这可能对你有帮助。您需要创建递归调用的函数

function getChild($conn, $id) {
    $rs=mysqli_query($conn, "SELECT * from test_tbl where parent = '$id' order by name ASC ");
    while($result = mysqli_fetch_assoc($rs)) {
        $current_parent_sequence = $result["sequence"];
        $parent = $result["parent"];
        echo "--" . $result["name"] . "<br>";
        if($parent != 0)
            getChild($conn, $current_parent_sequence);
    }
}

$rs=mysqli_query($conn, "SELECT * from test_tbl where parent = '' order by name ASC ");
while($result = mysqli_fetch_assoc($rs)) {
    $current_parent_sequence = $result["sequence"];
    $parent = $result["parent"];
    echo $result["name"] . "<br>";
    getChild($conn, $current_parent_sequence);
}
函数getChild($conn,$id){ $rs=mysqli_query($conn,“SELECT*fromtest_tbl,其中parent='$id'按名称排序ASC”); 而($result=mysqli\u fetch\u assoc($rs)){ $current_parent_sequence=$result[“sequence”]; $parent=$result[“parent”]; echo“--”$result[“name”]。“
”; 如果($parent!=0) getChild($conn,$current\u parent\u sequence); } } $rs=mysqli_query($conn,“SELECT*fromtest_tbl,其中parent=''按名称排序ASC”); 而($result=mysqli\u fetch\u assoc($rs)){ $current_parent_sequence=$result[“sequence”]; $parent=$result[“parent”]; echo$result[“name”]。“
”; getChild($conn,$current\u parent\u sequence); }
使用
mysqli
,因为
mysql
已被弃用。

好,所以第二个循环的问题是,在检查完所有
子项之前,它不会检查
子项,因此,在检查
子项时,它处于
子类别2
上,没有任何子项。类似@Apb建议的递归函数将帮助您检查每个条目的表,而不是在最后一个条目之后。
mysql.*
已被删除,而不是弃用。
function getChild($conn, $id) {
    $rs=mysqli_query($conn, "SELECT * from test_tbl where parent = '$id' order by name ASC ");
    while($result = mysqli_fetch_assoc($rs)) {
        $current_parent_sequence = $result["sequence"];
        $parent = $result["parent"];
        echo "--" . $result["name"] . "<br>";
        if($parent != 0)
            getChild($conn, $current_parent_sequence);
    }
}

$rs=mysqli_query($conn, "SELECT * from test_tbl where parent = '' order by name ASC ");
while($result = mysqli_fetch_assoc($rs)) {
    $current_parent_sequence = $result["sequence"];
    $parent = $result["parent"];
    echo $result["name"] . "<br>";
    getChild($conn, $current_parent_sequence);
}