1

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>';
            }
            } ?>
2
  • 2
    you should have use different variable for $row and $result. not the same for both loop. Commented May 16, 2016 at 6:26
  • i am not sure but there is may issues with in both query you used same $row variable can you please change that and try. Commented May 16, 2016 at 6:27

4 Answers 4

3

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>';
        }
    }
} 
?>
Sign up to request clarification or add additional context in comments.

1 Comment

That did it I thought that was the problem but wasn't sure if I could change the names. Thank you!!
1

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.