我有一个包含PRTL_BusRoute
的表格,其中涉及各个区域的ID
,AreaID
作为每个区域下的ID's
区域。
**ID** **BustrouteID** **AreaID**
0 1 3
1 1 5
1 1 6
2 2 5
3 2 6
4 2 8
5 3 9
6 3 12
7 4 1
8 4 8
9 4 11
巴士路线的名称存储在tbl_BusRoute
中,区域存储在dbo.GEN_Area
。我需要显示如下:
Zonename1
Areas coming under this zone in a list
Zonename2
Areas coming under this zone in a list
........
我的PHP代码是这样的:
$area = '';
$getbus = mssql_query("SELECT BusRouteID,AreaID FROM dbo.Acc_BusRouteDetail");
while($data_getbus = mssql_fetch_row($getbus)){
$getZone = mssql_query("SELECT Busroute FROM PRTL_BusRoute
WHERE BusRouteID = '$data_getbus[0]'");
while($data_getZone = mssql_fetch_row($getZone)){
$zone_name = "<h4>".$data_getZone[0]."</h4>";
}
$getArea = mssql_query("SELECT AreaName_1 FROM dbo.GEN_Area
WHERE AreaID = '$data_getbus[1]' ORDER BY AreaName_1");
while($data_getArea = mssql_fetch_row($getArea)){
$area .= $data_getArea[0]."</br>";
}
echo $zone_name."<br/>".$area."<br/>";
}
输出如下:
ZoneName1
Areaname1
ZoneName1
AreaName1
AreaName2
ZoneName1
AreaName1
Areaname2
AreaName3
ZoneName2
Areaname1
ZoneName2
Areaname1
Areaname2 so on
我想知道如果可能的话,我如何选择不同的busroutes以及它下面的所有区域。我没有逻辑如何选择单个路径ID,然后选择其下的所有区域
答案 0 :(得分:1)
我稍微更改SQL查询以立即获取所有数据,并且每次循环并打印该区域,而如果自上一行以来它已经更改,则只打印总线路径。像这样(未经测试的)代码;
$last_route = '';
$getbus = mssql_query("SELECT br.BusRoute,a.AreaName_1 FROM dbo.Acc_BusRouteDetail brd JOIN dbo.PRTL_BusRoute br ON br.busrouteid = brd.busrouteid JOIN dbo.GEN_Area a ON a.areaid = brd.areaid ORDER BY br.busroute, a.areaname_1");
while($data_getbus = mssql_fetch_array($getbus)){
if($last_route != $data_getbus['BusRoute']) {
$last_route = $data_getbus['BusRoute'];
echo '<h4>'.$last_route.'</h4>';
}
echo '<br/>'.$data_getbus['AreaName_1'].'</br>';
}