我有一个MySQL数据库
id area_name area_district area_shire
1 Harborne Birmingham West Midlands
2 Edgbaston Birmingham West Midlands
3 Halesowen Dudley West Midlands
4 Rugby Warwickshire
5 Nuneaton Warwickshire
如何显示,
West Midlands (main heading)
Birmingham (sub heading)
Harborne
Edgbaston
Dudley (sub heading)
Halesowen
Warwickshire (main heading)
Rugby
Nuneaton
是否有一个可以实现此目的的while循环?
编辑:以下是我到目前为止所尝试的内容:
$query = mysql_query("SELECT * FROM Areas_Covered ORDER BY ID"); //fetch the results
// convert results into an array
while($rows = mysql_fetch_array($query)):
$area_name = $rows ['area_name'];
$area_district = $rows ['area_district'];
$area_name_url = str_replace(' ', '_', $area_name);
echo "<a href=\"Driveway_Cleaning_$area_name_url.php\">$area_name</a><br>";
endwhile;
答案 0 :(得分:0)
好吧,您可能希望先将数据“标准化”,但这是另一个问题。
我会迭代行并检查更改。我会做类似的事情:
$area_shire = '';
$area_district = '';
$area_name = '';
while($rows = mysql_fetch_array($query)):
if($rows['area_shire'] != $area_shire) {
echo '<h1>'.$rows['area_shire'].'</h1>';
$area_shire = $rows['area_shire'];
}
/* do the same for district using h2 and name using h3 */
endwhile;