Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:44:07+00:00 2026-06-04T07:44:07+00:00

I have the following code to populate cities in a table. $cities_query = SELECT

  • 0

I have the following code to populate cities in a table.

$cities_query = "SELECT city_name FROM city_selection";
$cities_result = mysql_query($cities_query);


echo "<table>";
while ($row = mysql_fetch_assoc($cities_result))
{
    echo "<tr>";
    echo "<td>".$row['city_name'] . "</td>";
    echo "</tr>";
}
echo "</table>";

Now I need to group every city by a region so I created the table called city_region_selection and added a Foreign key from the new column "region_id" in the city_selection table to region_id in the new table.

I’m now struggling to present this data in a nice html table using PHP.

Is it even possible to populate first a table row with region_name and then some subrows containing cities from that region? Please see this image for details:

http://oi48.tinypic.com/2uq0z9x.jpg

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:44:08+00:00Added an answer on June 4, 2026 at 7:44 am

    You just have to do a little preprocessing:

    /* SQL
       SELECT
          city_name,
          region_name
       FROM
          city_selection
          NATURAL JOIN city_region_selection
    SQL */
    
    $regions = array();
    while ($result->fetch()) {
       if (!isset($regions[$result->region_name])) {
          $regions[$result->region_name] = array();
       }
       $regions[$result->region_name][] = $result->city_name;
    }
    

    Alternate Method with SQL focus

    /* SQL
       SELECT
          region_name,
          GROUP_CONCAT(city_name SEPARATOR ',') cities
       FROM
          city_selection
          NATURAL JOIN city_region_selection
       GROUP BY
          region_name
    */
    
    $regions = array();
    while ($result->fetch()) {
       $regions[$result->region_name] = explode(',', $result->city_name);
    }
    

    Help

    Note that the while syntax is just abridged. You would probably replace it with

    while ($row = mysql_fetch_assoc($cities_result)) {
       $regions[$row['region_name']] = explode(',', $row['city_name']);
    }
    

    … for example

    Once regions is built, simply do this:

    foreach ($regions as $region => $cities) {
       echo "<tr><td>$region</td></tr>";
       foreach ($cities as $city) {
          echo "<tr><td class="tabbed">$city</td></tr>";
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to populate a list from my database. I have the following code:
I have the following code to populate an array of strings, but every time
I have the following code which is loading an xml file to populate a
I have the following code which is meant to populate a dropdown with a
I have the following code to launch a dilog, do ajax and populate it
I have the following code that loads a plist file from the resource folder
I'm using the following code to populate a DataGridView (sqliteAdapter derives from DbDataAdapter): sqliteAdapter.SelectCommand.CommandText
I have the following code, which works fine if you just want to populate
I have the following code to populate the UITableView with the tweets. - (UITableViewCell
I have the following code that I'm trying to use to populate a ComboBox,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.