简体   繁体   中英

Display a list of cities and the names of the locations in those cities

My database looks like this...

id, city, name

1, ajo, belly acres

2, alpine, alpine village

3, apache junction, lost dutchman

4, apache junction, butterfield

5, apache junction, benson

6, bisbee, queen mine

... and so on

I'm trying to display the list like...

<section>
  <h1>ajo</h1>
  <h2>belly acres</h2>
</section>
<section>
  <h1>alpine</h1>
  <h2>alpine village</h2>
</section>
<section>
  <h1>apache junction</h1>
  <h2>lost dutchman</h2>
  <h2>butterfield</h2>
  <h2>benson</h2>
</section>
<section>
  <h1>bisbee</h1>
  <h2>queen mine</h2>
</section>

The code I am currently using works fine for cities with only one name, but in the example of Apache Junction which has more than one name, I get this..

<section>
  <h1>apache junction</h1>
</section>
  <h2>lost dutchman</h2>
  <h2>butterfield</h2>
  <h2>benson</h2>
<section>
  <h1>bisbee</h1>
  <h2>queen mine</h2>
</section>

I want to display all of the location names in Apache junction in one section. This is the code I am using...

    $previousLevel = "";
    $city_query = $wpdb->get_results( $wpdb->prepare("SELECT name, city FROM locations") );
    foreach ($city_query as $city_list) {
        $city = $city_list->city;
        $name = $city_list->name;
        if ($city != $previousLevel) {
            $dump_list .= '<section><h1>' . $city . '</h1>';
            $dump_list_end = '</section>';
        }
        $dump_list .= '<h2><a href="#">' . $name . '</a></h2>';
        $dump_list .= $dump_list_end;
        $previousLevel = $city; 
    }

Any help would be appreciated.

Could you give this modified code a try?

$previousLevel = "";
$city_query = $wpdb->get_results( $wpdb->prepare("SELECT name, city FROM locations") );
foreach ($city_query as $city_list) {
    $city = $city_list->city;
    $name = $city_list->name;
    if ($city != $previousLevel) {
        if ($previousLevel != "") {
            $dump_list .= '</section>';
        }
        $dump_list .= '<section><h1>' . $city . '</h1>';
        $previousLevel = $city;
    }
    $dump_list .= '<h2><a href="#">' . $name . '</a></h2>';
}

Modified code, longer but simpler to understand and with proper data escaping:

$city_query = $wpdb->get_results( $wpdb->prepare("SELECT name, city FROM locations ORDER BY city, name") );
$cities = array();
foreach ($city_query as $city_list) {
    if (!isset($cities[$city_list->city])) {
        $cities[$city_list->city] = array();
    }
    $cities[$city_list->city][] = $city_list->name;
}
$dump_list = '';
foreach ($cities as $city => $names) {
    $dump_list .= '<section><h1>' . htmlspecialchars($city) . '</h1><h2>';
    $dump_list .= implode('</h2><h2>', array_map('htmlspecialchars', $names));
    $dump_list .= '</section>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM