哈哈,我做过了,VF代码
*
*/
//定义一个新类
//实现公交换乘
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;$ipassline($start);
$endpass = $this->passline($end);
//得到经过起点和终点的线路的全部组合
$resultbus = "";
for($a=0;$abussperline($endpass[$b]);
for($c=0;$cbussperline($resultbus_ok2[1]);
//显示
echo ("");
echo ("" .$bus1[0]. ""); echo ("" .$resultbus_ok2[0]. "");
echo ("" .$resultbus_ok2[2]. " ==> ");
echo ("" .$resultbus_ok2[1]. "");
echo ("" .$bus2[count($bus2)-1]. "");
echo("");
}
echo("");
}
}
else{
//有直达车,直接显示直达车情况
echo ("");
echo ("");
echo ("车次");
echo ("起点");
echo ("经过");
echo ("经过");
echo ("终点");
echo ("详情");
echo ("");
$strTemp = substr($strTemp,0,strlen($strTemp)-2);//去掉最右边的"||"
$strTemp_ok1 = explode("||",$strTemp);//将字符串分割成数组
for($nn=0;$nnbussperline($strTemp_ok1[$nn]);
//显示
echo ("");
echo ("" .$strTemp_ok1[$nn]. "");
echo ("" .$bus[0]. "");
echo ("" .$start. " ==> ");
echo ("" .$end. "");
echo ("" .$bus[count($bus)-1]. "");
echo ("详情");
echo("");
}
echo("");
}
}
}
/*
定义好抽象类后,使用就非常简单了
*/
$bus = new buss;
$bus->bussStationToStation("火车站","五里墩");
//一切ok,直接就可以看到结果了
?