-2

我有一个 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;
4

1 回答 1

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;
于 2012-12-05T22:11:21.803 回答