I have three different status for the train as below,
1.Train Not Started
2.Train Departed
3.Train Not Reached.
In the below php code i am getting only the two outputs below using ternary operator.
Train Departed and Train Not Reached.
Code: Select all
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Train Status Time Table</TITLE>
<style>
table { width:60%; }
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
table#t01 tr:nth-child(even) {
background-color: white;
width:60%;
}
table#t01 tr:nth-child(odd) { background-color:white; }
table#t01 th {
background-color: blue;
color: white;
}
table#t02 th {
background-color: blue;
color: white;
}
body { background-color: lightblue; }
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
width: auto;
}
li { float: left; }
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) { background-color: #111; }
.active { background-color: #4CAF50; }
</style>
</HEAD>
<BODY>
<CENTER><H2><B>Live Train Running Status</B></H2></CENTER>
<UL>
<LI><A class="active" href="#home">Home</A></LI>
<LI><A href="#news">Live Train Status</A></LI>
<UL style="float:right;list-style-type:none;">
<LI><A href="#about">Blog</A></LI>
<LI><A href="#login">Contact</A></LI>
</UL>
</UL>
<BR />
<CENTER>
<?php
$json = '';
if(isset($_GET['trainnumber']) && (isset($_GET['doj']))) {
$url = 'http://api.railwayapi.com/live/train/'.$_GET['trainnumber'].'/doj/'.$_GET['doj'].'/apikey/*/';
$url_trainname = 'http://api.railwayapi.com/name_number/train/'.$_GET['trainnumber'].'/apikey/*/';
$json = json_decode(file_get_contents($url), true);
$json_trainname = json_decode(file_get_contents($url_trainname), true);
$trainname=$json_trainname ['train']['name'];
$trainnumber = $json['train_number'];
$doj = $json['route'][0]['scharr_date'];
$position = $json['position'];
?>
<TABLE id='t01'>
<TR><TH>Train Number</TH><TH>Date on Journey</TH></TR>
<TR><TD><?= $trainnumber; ?></TD><TD><?= $doj; ?></TD></TR>
<TR><TH>Position</TH></TR>
<TR><TD><?= $position; ?></TD></TR>
<TR><TH>Train Name</TH></TR>
<TR><TD><?= $trainname; ?></TD></TR>
</TABLE>
<TABLE id='t01'>
<TR><TH>Station</TH><TH>Scheduled</TH><TH>Actual</TH><TH>Status/Delay</TH></TR>
<?php
foreach($json['route'] as $stop) {
$url_trainnameconvert = 'http://api.railwayapi.com/code_to_name/code/'.$stop['station'].'/apikey/neoid4725/'; //passing the station element from Json Response 1 to Json Response 2
$json_trainnameconvert = json_decode(file_get_contents($url_trainnameconvert), true);
$maxname=$json_trainnameconvert['stations'][0]['fullname'];
($stop['has_departed'] === true) ? $stop['has_departed'] = 'Departed' : $stop['has_departed'] = 'Still not reached here'; // Train Departed and Train Not Reached status displayed here.here i want one more status as train not started with delay time.
echo ' <TD>'.$maxname.'</TD><TD>'.$stop['scharr'].'</TD><TD>'.$stop['actarr'].'</TD><TD>'.$stop['has_departed']."</TD></TR>\n";
}
} else {
echo 'Something went wrong, please notify to admin ';
}
?>
</TABLE>
</CENTER>
</BODY>
</HTML>
I want to check the status one by one and produce the result .
1. $position has the text as Train is currently at Source and late by 0 minutes then it should print the Not Started as output.
else
2.$stop['has_departed'] === true and $stop['latemin'] === 0 mins late then it should print the status as departed
else
3.$stop['has_departed'] === true and !$stop['latemin'] === 0 mins late then it should print the status as departed/latemin
else
4.if the above conditions not okay it should print the value as Not reached.
Kindly help me to find the solution.