//------------------------------------------------------------------------------ // Train information page. // Created at 4:03 PM 10/7/2006 // Input parameters: // no: train numbers, a string // from: the city from // to: the destination / via city // // // // //------------------------------------------------------------------------------ include_once("cache_begin.php"); $q = $HTTP_GET_VARS['q']; $id = $HTTP_GET_VARS['id']; $trainno = $HTTP_GET_VARS['trainno']; $seq = $HTTP_GET_VARS['seq']; $page = $HTTP_GET_VARS['page']; $stationno = $HTTP_GET_VARS['stationno']; $stationname = $HTTP_GET_VARS['stationname']; $arrv = $HTTP_GET_VARS['arrv']; $dept = $HTTP_GET_VARS['dept']; $dist = $HTTP_GET_VARS['dist']; $via = $HTTP_GET_VARS['via']; $from = $HTTP_GET_VARS['from']; $to = $HTTP_GET_VARS['to']; $action = $HTTP_GET_VARS['action']; include_once("template.php"); $L_base = "http://home.wangjianshuo.com/train/"; $vias = split(',', $via); function format_time($t) { return $t; if(strlen($t) != 4) return $t; return substr($t, 0, 2) . ":" . substr($t, 2, 2); } function travel_time($dept, $arrv) { if(strlen($dept) != 4) return 0; if(strlen($arrv) != 4) return 0; $depttime = substr($dept,0, 2) * 60 + substr($depart, 2, 2); $arrvtime = substr($arrv,0, 2) * 60 + substr($arrv, 2, 2); $travel = $arrvtime - $depttime; if($travel<0) $travel = $travel + 24 * 60; $tmin = $travel % 60; $thour = ($travel - $tmin) / 60; return $thour . ":" . str_pad($tmin, 2, "0", STR_PAD_LEFT); } if($trainno == '' && $from == '' && $stationname == '') { $title = "China Train Schedule"; $htmlintro = '
Major Stations
Shanghai Beijing Nanjing Hangzhou Tianjin Suzhou
Major Train Lines
Shanghai to Nanjing via Suzhou, Wuxi, Changzhou, Zhenjiang
Shanghai to Hangzhou via Jiaxing
'; $template =& new Template("train.tmpl"); $template->AddParam('htmlintro', $htmlintro); $template->AddParam('Title', $title); $template->EchoOutput(); exit; } $sqlwhere = ' 1 = 1 '; if($trainno != '') { if(substr($trainno, -1, 1) == '%') $sqlwhere .= " AND trainno LIKE '$trainno'"; else $sqlwhere .=" AND trainno = '$trainno' "; $title = "Train # $trainno"; } if($stationname != '') { $sqlwhere .=" AND stationname = '$stationname' "; $title = "$stationname Station"; } mysql_connect('localhost', 'wangjian_public', 'Password01!'); mysql_select_db('wangjian_public'); if($from != '' && $to !='') { $title = "Train from $from to $to"; if($via != '') $title .= " via " . implode(', ', array_reverse($vias)); $htmlintro = ""; if($via != '') { $rvias = array_reverse($vias); $htmlintro = "Train from $to to $from via " . implode(', ', $rvias) . "
"; } $htmlintro = "$htmlintro |
Train # | $from ->"; if($via != '') foreach($vias as $v) $htmltable .= " | $v ->"; $htmltable .= " | $to | Duration | Distance (km)";
$sql = "SELECT * FROM train_trains WHERE stationname LIKE '$from' ORDER BY dept, trainno, seq LIMIT 500";
$results = mysql_query($sql) or die ("Sorry: " . mysql_error() . " $sql"); while($line = mysql_fetch_array($results)) { $ismatch = false; $res = mysql_query("SELECT * FROM train_trains WHERE trainno = '{$line['trainno']}'"); $linestations = array(); while($ln = mysql_fetch_array($res)) { $linestations[$ln['stationname']] = $ln['dept']; if(strtolower($ln['stationname']) == strtolower($to) && $line['seq'] < $ln['seq']) { $tag_isstart = $line['seq'] == 1 ? ">" : " "; $tag_isend = (mysql_num_rows($res) == count($linestations)) ? "<":" "; $ismatch = true; $htmlvia = ''; if($via != '') foreach($vias as $v) $htmlvia .= " | " . $linestations[$v]; $htmltable .= " |
{$line['trainno']} | " . "$tag_isstart{$line['dept']}". $htmlvia . " | {$ln['arrv']}$tag_isend". " | " . travel_time($line['dept'], $ln['arrv']) . " | " . ($ln['dist'] - $line['dist']); } } } $htmltable .= " |
Train # | Seq | Station Name | Arrive | Depart | Distance"); $template->EchoOutput(); ?> include_once("cache_end.php"); ?> |