* **********************************************************************************************
* id(自動編號 pk) busline(公交線路 int 4) busname(站點名稱 varchar 20) busorder(站點順序 int 4)
* 1 1 火車站 1
* 2 1 勝利廣場 2
* 3 1 賣漁橋 3
* ... ... ... ...
* 25 2 勝利廣場 1
* 26 2 天香電器城 2
* 27 2 五裏墩 3
* ... ... ... ...
* 數據庫就是這樣插記錄的,可以把城市的公交線路數據全部插進去
************************************************************************************************
*/
//定義壹個新類
//實現公交換乘
class buss{
//定義數據庫連接成員變量
var $host;
var $user;
var $passwd;
var $database;
var $conn;
//利用構造函數實現變量初始化,連接數據庫
function buss(){
$this->host="localhost";
$this->user="root";
$this->passwd="";
$this->database="bus";
$this->conn=mysql_connect($this->host, $this->user,$this->passwd) or
die("Could not connect to $this->host");
mysql_select_db($this->database,$this->conn) or
die("Could not switch to database $this->database");
}
//統計數據庫中所有公交站點名,存入數組
//返回站點名
function busstotal(){
$SQL = "select * from bus group by busname";
$count = 0;
$result = mysql_query($SQL);
while($row = mysql_fetch_object($result)){
$bustotal[$count]= $row->busname;
$count++;
}
return $bustotal;
}
//統計數據庫中所有公交路線,存入數組
//返回公交線路
function busslinetotal(){
$SQL = "select * from bus group by busline";
$count = 0;
$result = mysql_query($SQL);
while($row = mysql_fetch_object($result)){
$buslinetotal[$count]= $row->busline;
$count++;
}
return $buslinetotal;
}
//統計數據庫中每壹線路經過的站點,存入數組
//需要參數line,區別每壹路車
//返回站點名
function bussperline($line){
$SQL = "select * from bus where busline = '$line'";
$count = 0;
$result = mysql_query($SQL);
while($row = mysql_fetch_object($result)){
$busperline[$count]= $row->busname;
$count++;
}
return $busperline;
}
//統計經過某站點的所有公交車的組合
//需要參數station,表示經過的站點
//返回公交線路
function passline($station){
$SQL = "select * from bus where busname = '$station' group by busline";
$count = 0;
$result = mysql_query($SQL);
while($row = mysql_fetch_object($result)){
$passline[$count]= $row->busline;
$count++;
}
return $passline;
}
//實現換乘算法的函數
//需要提供參數,查詢的起點和終點
function bussStationToStation($start,$end){
$flag1 = false;
$flag2 = false;
//函數回調
$busstotal = $this->busstotal();
$busslinetotal = $this->busslinetotal();
//判斷數據庫中是否有此站點
for($i=0;$i<count($busstotal);$i++){
if($start==$busstotal[$i]) $flag1 = true;
if($end==$busstotal[$i]) $flag2 = true;
if($flag1 and $flag2) break;
}
//有壹個站點不存在
if(!($flag1 and $flag2)){
if(!$flag1) die("$start站點不存在!");
if(!$flag2) die("$end站點不存在!");
}
//兩個站點都存在的情況
//首先判斷有無直達車
$strTemp = "";
//遍歷所有車次
for($i=0;$i<count($busslinetotal);$i++){
$flag3 = 0;
//函數回調
$bussperline = $this->bussperline($busslinetotal[$i]);
//遍歷每壹車次經過的站點
for($j=0;$j<count($bussperline);$j++){
if($start==$bussperline[$j]) $flag3 +=1;
if($end==$bussperline[$j]) $flag3 +=1;
if($flag3==2) break;
}
if($flag3==2)
//保存直達車次,以||分割
$strTemp = $strTemp.$busslinetotal[$i]."||";
}
if($strTemp==""){
//沒有直達車,則計算壹次換乘情況
echo("<strong><font color=#FF0000>".$start. "</font></strong> 到 <strong><font color=#FF0000>"
.$end."</font></strong> 沒有直達車!請參照下列換乘建議.
");
//查詢壹級中轉站
//start起點
//end終點
//函數回調,取得經過起點和終點的所有組合
$statpass = $this->passline($start);
$endpass = $this->passline($end);
//得到經過起點和終點的線路的全部組合
$resultbus = "";
for($a=0;$a<count($statpass);$a++){
for($b=0;$b<count($endpass);$b++){
//判斷兩條線路有沒有交叉點
$startper = $this->bussperline($statpass[$a]);
$endper = $this->bussperline($endpass[$b]);
for($c=0;$c<count($startper);$c++){
for($d=0;$d<count($endper);$d++){
if($startper[$c]==$endper[$d]){
//成功找到交叉點後
//存儲交叉點處信息
//此只為壹次換乘
$fistid = $statpass[$a];
$secondid = $endpass[$b];
$changestation = $startper[$c];
$resultbus .= $fistid.";".$secondid.";".$changestation."||";
}
}
}
}
}
if($resultbus=="")
{
//沒有找到換乘線路
echo("
抱歉,<strong><font color=#FF0000>" .$start. "</font></strong> 到 <strong><font color=#FF0000>"
.$end. "</font></strong>沒有直達車,換乘壹次也無法到達!");
}
else{
//找到換乘線路
$resultbus = substr($resultbus,0,strlen($resultbus)-2);//去掉最右邊的"||"$resultbus_ok1 = explode("||",$resultbus);//將字符串分割成數組
echo ("<table width=600 border=0 bgcolor=#003399 cellpadding=3 cellspacing=1>");
echo ("<tr bgcolor=#FDDD90>");
echo ("<td width=150><strong>起點</strong></td>");
echo ("<td width=70><strong>車次</strong></td>");
echo ("<td width=160><strong>中轉站</strong></td>");
echo ("<td width=70><strong>車次</strong></td>");
echo ("<td width=150><strong>終點</strong></td>");
echo ("</tr>");
for($mm=0;$mm<count($resultbus_ok1);$mm++){
$resultbus_ok2 = explode(";",$resultbus_ok1[$mm]);
//計算兩輛車的起點和終點
$bus1 = $this->bussperline($resultbus_ok2[0]);
$bus2 = $this->bussperline($resultbus_ok2[1]);
//顯示
echo ("<tr bgcolor=#E8F3FF onMouseOver = this.style.backgroundColor = '#FFF9EE' onMouseOut = this.style.backgroundColor = ''>");
echo ("<td width=150><strong><font color=#FF0000>" .$bus1[0]. "</strong></font></td>"); echo ("<td width=70><a href=''>" .$resultbus_ok2[0]. "</a></td>");
echo ("<td width=160><strong><font color=#FF0000>" .$resultbus_ok2[2]. "</strong></font> ==> </td>");
echo ("<td width=70><a href=''>" .$resultbus_ok2[1]. "</a></td>");
echo ("<td width=150><strong><font color=#FF0000>" .$bus2[count($bus2)-1]. "</strong></font></td>");
echo("</tr>");
}
echo("</table>");
}
}
else{
//有直達車,直接顯示直達車情況
echo ("<table width=600 border=0 bgcolor=#003399 cellpadding=3 cellspacing=1>");
echo ("<tr bgcolor=#FDDD90>");
echo ("<td width=150><strong>車次</strong></td>");
echo ("<td width=70><strong>起點</strong></td>");
echo ("<td width=160><strong>經過</strong></td>");
echo ("<td width=70><strong>經過</strong></td>");
echo ("<td width=150><strong>終點</strong></td>");
echo ("<td width=150><strong>詳情</strong></td>");
echo ("</tr>");
$strTemp = substr($strTemp,0,strlen($strTemp)-2);//去掉最右邊的"||"
$strTemp_ok1 = explode("||",$strTemp);//將字符串分割成數組
for($nn=0;$nn<count($strTemp_ok1);$nn++){
//計算車輛的起點和終點
$bus = $this->bussperline($strTemp_ok1[$nn]);
//顯示
echo ("<tr bgcolor=#E8F3FF onMouseOver = this.style.backgroundColor = '#FFF9EE' onMouseOut = this.style.backgroundColor = ''>");
echo ("<td width=150><font color=#FF0000>" .$strTemp_ok1[$nn]. "</strong></font></td>");
echo ("<td width=70><a href=''>" .$bus[0]. "</a></td>");
echo ("<td width=160><strong><font color=#FF0000>" .$start. "</strong></font> ==> </td>");
echo ("<td width=70><a href=''>" .$end. "</a></td>");
echo ("<td width=150><font color=#FF0000>" .$bus[count($bus)-1]. "</strong></font></td>");
echo ("<td width=70><a href=''>詳情</a></td>");
echo("</tr>");
}
echo("</table>");
}
}
}
/*
定義好抽象類後,使用就非常簡單了
*/
$bus = new buss;
$bus->bussStationToStation("火車站","五裏墩");
//壹切ok,直接就可以看到結果了
>