所以我正在尝试使用WordPress中的for_each自定义代码循环,以便为每个具有州名称的表显示代码(城市)..我的结果将如此显示;
- New York
-- city1
-- city2
-- city3
- New Jersey
-- city1
-- city2
等等..在数据库(mysql)中,每个条目都放在一行,其中包含State和Table for City但在同一行...
我尝试过以下操作,但我没有参与其中;
$states = $wpdb->get_row("SELECT * FROM $wpdb->wp_em_locations WHERE location_state=*);
$city = $wpdb->get_row("SELECT * FROM $wpdb->wp_em_locations WHERE location_town=*);
foreach ( $states as $state ) {
echo '$state';
echo '$city';
}
答案 0 :(得分:0)
编辑:get_row可能不是获得此信息的最佳方法。我正在使用您的上下文来获取此信息,但这可能是您正在寻找的方法。
$states = $wpdb->get_results("SELECT location_state FROM wp_em_locations GROUP BY location_state");
foreach ( $states as $state ) {
echo $state->location_state;
$cities = $wpdb->get_results("SELECT location_town FROM wp_em_locations WHERE location_state = '" . $state . "'");
foreach ($cities as $city) {
echo $city->location_town;
}
}
参考:https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results