做一个PHP而在循环里面循环

时间:2016-05-16 06:23:59

标签: php loops while-loop

我正在尝试在另一个php while循环中执行php while循环但是当我到达第二个循环时它不会返回到第一个循环并再次重新循环这里是代码我正在使用:

数据库连接字符串位于单独的模块中。第一个while循环应循环两次,但我认为当它到达第二个数据库while循环时该循环返回正数并且它影响第一个循环,所以我只从第一个循环得到1个循环。有人可以告诉我如何改变这一点以避免这个问题?

以下是数据库中的两个循环:

while($row = $result->fetch_assoc()) {

    // Show/Hide Regions:

    error_reporting(-1);
    ini_set('display_errors', 'On');

    //Access our class.
    $db_Class = new db_Class;
    $conn = ($db_Class->db_conn());

    $sql = "SELECT id, region FROM tbl_region;";
    $result = $conn->query($sql);

          if ($result->num_rows > 0) {
        // output data of each row
        $counter = 0; //Set count to 1
        while($row = $result->fetch_assoc()) {
            $counter++; //Increment counter by 1's
            $rID = $row["id"];
            ?>
                Output Region Here!

               <?php 

            //Output companies for this region.

            $sql = "SELECT
                    tbl_company.company_name,
                    tbl_company.group_number,
                    tbl_region.region
                    FROM
                    tbl_region
                    INNER JOIN tbl_company ON tbl_company.region_id = tbl_region.id
                    WHERE
                    $rID = tbl_company.region_id
                    ORDER BY
                    tbl_company.company_name ASC
                    ";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {

                    echo $row["company_name"]."<br>";

                    }
                }
            echo "A";
            echo '</div>';
            }
            } ?>

4 个答案:

答案 0 :(得分:3)

使用任何其他名称更改内部循环$ row和$ result。例如,我在这里完成了$ rowInner和$ resultInner。

<?php 
while($row = $result->fetch_assoc()) {
    if ($result->num_rows > 0) {
        $counter = 0; //Set count to 1
        while($row = $result->fetch_assoc()) {
            $counter++; //Increment counter by 1's
            $rID = $row["id"];
            ?>
                Output Region Here!

            <?php 
            $sql = "SELECT
                    tbl_company.company_name,
                    tbl_company.group_number,
                    tbl_region.region
                    FROM
                    tbl_region
                    INNER JOIN tbl_company ON tbl_company.region_id = tbl_region.id
                    WHERE
                    $rID = tbl_company.region_id
                    ORDER BY
                    tbl_company.company_name ASC
                    ";
            $resultInner = $conn->query($sql);
            if ($resultInner->num_rows > 0) {
                while($rowInner = $resultInner->fetch_assoc()) { // Change $row to $rowInner
                    echo $rowInner["company_name"]."<br>";

                }
            }
            echo "A";
            echo '</div>';
        }
    }
} 
?>

答案 1 :(得分:1)

你应该为$result使用另一个变量,例如$result2 for while循环。这导致了冲突。

答案 2 :(得分:0)

你用第二个$ row / $结果覆盖了第一个$ row / $结果。尝试为第二组结果使用不同的名称。

// Show/Hide Regions:

error_reporting(-1);
ini_set('display_errors', 'On');

//Access our class.
$db_Class = new db_Class;
$conn = ($db_Class->db_conn());

$sql = "SELECT id, region FROM tbl_region;";
$result = 

$conn->query($sql);

  if ($result->num_rows > 0) {
// output data of each row
$counter = 0; //Set count to 1
while($row = $result->fetch_assoc()) {
    $counter++; //Increment counter by 1's
    $rID = $row["id"];
    ?>
        Output Region Here!

       <?php 

    //Output companies for this region.

    $sql = "SELECT
            tbl_company.company_name,
            tbl_company.group_number,
            tbl_region.region
            FROM
            tbl_region
            INNER JOIN tbl_company ON tbl_company.region_id = tbl_region.id
            WHERE
            $rID = tbl_company.region_id
            ORDER BY
            tbl_company.company_name ASC
            ";
    $result2 = $conn->query($sql);

    if ($result2->num_rows > 0) {
        // output data of each row
        while($row2 = $result2->fetch_assoc()) {

            echo $row2["company_name"]."<br>";

            }
        }
    echo "A";
    echo '</div>';
    }
    } ?>

答案 3 :(得分:0)

您应该为内部和外部while循环使用不同的变量。请尝试以下方法:

<?php
    // Show/Hide Regions:

    error_reporting(-1);
    ini_set('display_errors', 'On');

    //Access our class.
    $db_Class = new db_Class;
    $conn = ($db_Class->db_conn());

    $sql = "SELECT id, region FROM tbl_region;";
    $resultOuter = $conn->query($sql);

    if ($resultOuter->num_rows > 0) 
    {
        // output data of each row
        $counter = 0; //Set count to 1
        while($rowOuter = $resultOuter->fetch_assoc()) 
        {
            $counter++; //Increment counter by 1's
            $rID = $rowOuter["id"];

            //Output companies for this region.

            $sql = "SELECT tbl_company.company_name, tbl_company.group_number, tbl_region.region
                    FROM tbl_region
                    INNER JOIN tbl_company ON tbl_company.region_id = tbl_region.id
                    WHERE
                    $rID = tbl_company.region_id
                    ORDER BY
                    tbl_company.company_name ASC ";

            $resultInner = $conn->query($sql);

            if ($resultInner->num_rows > 0) 
            {
                // output data of each row
                while($rowInner = $resultInner->fetch_assoc()) 
                {
                    echo $rowInner["company_name"]."<br>";

                }
            }
            echo "A";
            echo '</div>';
        }
    } 
?>