Frank G.
Frank G.

Reputation: 1579

Doing a PHP While Loop Inside A While Loop

I am trying to do a php while loop inside another php while loop but when I get to the second loop it doesn't go back to the first one and reloop again Here is the code I am using:

Database connection string is in a separate module. The first while Loop should loop twice but what I think is happening when it gets to the second database while loop that loop returns positive and it affects the first loop so I only get 1 loop from the first loop. Can someone please tell me how I could change this to avoid this problem?

These are the two loops below from a database:

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>';
            }
            } ?>

Upvotes: 1

Views: 10714

Answers (4)

Mueyiwa Moses Ikomi
Mueyiwa Moses Ikomi

Reputation: 1142

You should use a different variable for $result like $result2 for 2 while loops. That is causing the conflict.

Upvotes: 1

Suyog
Suyog

Reputation: 2482

You should use different variables for inner and outer while loops. Try the following:

<?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>';
        }
    } 
?>

Upvotes: 0

Alan Tan
Alan Tan

Reputation: 347

You overwritten the first $row/$result with the second $row/$result. Try using a different name for the second set of results.

// 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>';
    }
    } ?>

Upvotes: 0

Dhara Parmar
Dhara Parmar

Reputation: 8101

Change inner loop $row and $result with any other name..for example I have done $rowInner and $resultInner here.

<?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>';
        }
    }
} 
?>

Upvotes: 3

Related Questions