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