I have created this PHP script, pulled together from other people's snippets and for the most part it works. However, the "live flights" table has something weird going on and I can't seem to fix it and wondered if anyone here might be able to help.
The issue is with the "Pilot" details. I have a piece of code which should, for each pilot pirep, identify their ID and then load their first and last name. This is exactly the same piece of code for the second table (recent flights) and that section works perfectly. For some reason, in the first table, things work fine when there is only one pilot flying. As soon as I get two pilots flying the table shows the pilot names to all be the same and it's not necessarily the name of a pilot that is actually flying at the time.
I have pasted the tables below. The coding forms part of a virtual airline management system called PHPVMS and is based around PHP, Dataclasses and MySQL. You can see the page itself at www.virginatlanticvirtual.co.uk
My current thinking is that because the first and second flight tables are using the same piece of code, this might be causing the conflict but I'm not technical enough to work out how to stop the conflict or change the code of one so it's done differently.
This is where I'm hoping the community can help me. If there is anything else you need to know or questions you have, please let me know.
Thanks in advance.
<!-- Start of live flights table -->
<div class="col-md-12 page-content">
<h2>Live Flights</h2>
<div class="stats-table">
<table>
<tr>
<th>Pilot</th>
<th>Flight</th>
<th>Departure</th>
<th>Arrival</th>
<th>Aircraft</th>
<th>Status</th>
</tr>
<?php $results=A CARSData::GetACARSData(); if (count($results)>0) { foreach($results as $flight) { ?>
<tr>
<?php $count=1 0; $pireps=P IREPData::getRecentReportsByCount($count); ?>
<?php if($flight->phasedetail == "Boarding") { echo "
<img style='padding-left:3px;' src=''>"; } elseif($flight->phasedetail == "Arrived") { echo "
<img style='padding-left:3px;' src=''>"; } elseif($flight->phasedetail == "On Approach") { echo "
<img style='padding-left:3px;' src=''>"; } ?>
<?php foreach ($pireps as $pirep) { $pilotinfo=P ilotData::getPilotData($pirep->pilotid); $pilotid = PilotData::getPilotCode($pilotinfo->code, $pilotinfo->pilotid); } ?>
<td>
<?php echo '<a href="'.SITE_URL. '/index.php/profile/view/'.$pilotinfo->pilotid.'">'.$pilotinfo->firstname.' '.$pilotinfo->lastname.'</a>';?></td>
<td>
<?php echo $flight->flightnum;?></td>
<td>
<?php echo $flight->depname;?></td>
<td>
<?php echo $flight->arrname;?></td>
<td>
<?php echo $flight->aircraftname;?></td>
<td>
<?php if($flight->phasedetail != 'Paused') { echo $flight->phasedetail; } else { echo "Cruise"; }?></font>
</td>
</tr>
<?php } } else { ?>
<tr>
<td width="20%" align="center" colspan="6" style="padding: 5px; font-size: 13px; font-weight: bold; color: #3399FF;">No Flights in Progress!</td>
</tr>
<?php } ?>
</table>
</div>
</div>
<!-- Start of recent flights table -->
<div class="col-md-12 page-content">
<h2>Recent Flights</h2>
<div class="stats-table">
<table>
<tr>
<th>Flight</th>
<th>Pilot</th>
<th>Departure</th>
<th>Arrival</th>
<th>Aircraft</th>
<th>Duration</th>
<th>V/S</th>
<th>Info</th>
</tr>
<?php $count=1 0; $pireps=P IREPData::getRecentReportsByCount($count); ?>
<?php if (count($results)>0); if (count($pireps) > 0) { foreach ($results as $flight); foreach ($pireps as $pirep) { $pilotinfo = PilotData::getPilotData($pirep->pilotid); $pilotid = PilotData::getPilotCode($pilotinfo->code, $pilotinfo->pilotid); $acrid = OperationsData::getAircraftByReg($pirep->registration);
$results = ACARSData::GetACARSData(); $fcode = substr($flight->flightnum, 0, 3); echo '
<tr>'; echo '
<td><a href="'.SITE_URL.'/index.php/pireps/viewreport/'.$pirep->pirepid.'">'.$pirep->code.$pirep->flightnum.'</a>
</td>'; echo '
<td><a href="'.SITE_URL.'/index.php/profile/view/'.$pilotinfo->pilotid.'">'.$pilotinfo->firstname.' '.$pilotinfo->lastname.'</a>
</td>'; echo '
<td>'.$pirep->depicao.'</td>'; echo '
<td>'.$pirep->arricao.'</td>'; echo '
<td>'.$pirep->aircraft.'</td>'; echo '
<td>'.$pirep->flighttime.'</td>'; echo '
<td>'.$pirep->landingrate.' ft/m</td>'; if($pirep->accepted == PIREP_ACCEPTED) echo '
<td><span class="label label-important"><font color="green">Accepted</font></span>
</td>'; elseif($pirep->accepted == PIREP_REJECTED) echo '
<td><span class="label label-important"><font color="red">Rejected</font></span>
</td>'; elseif($pirep->accepted == PIREP_PENDING) echo '
<td><span class="label label-warning"><font color="orange">Pending</font></span>
</td>'; elseif($pirep->accepted == PIREP_INPROGRESS) echo '
<td>On Progress</td>'; echo '</tr>'; } } else { echo '
<tr>
<td>There are no recent Flights!</td>
</tr>'; } ?>
</table>
</div>
<!-- End of recent flights table -->
</div>
<!-- Start of booked flights table -->
<div class="col-md-12 page-content">
<h2>Booked Flights</h2>
<div class="stats-table">
<?php MainController::Run( 'FrontBids', 'RecentFrontPage', 10); ?>
</div>
</div>
</div>
There is a flawed linkage between your flights and pilots.
The pilot isn't being fetched using information from the $flight
object. It's just fetching the same static list, over and over, and choosing the final element each time. ;)
In each iteration of $results
(where you look at a single $flight
), you request the first 10 results from the recent reports (PIREPData::getRecentReportsByCount($count)
). That list doesn't change; each $flight
looks at the same 10 reports.
Also, the loop that looks at $pireps
always results in $pilotinfo
equaling the final element of $pireps
. The 10th pilot will always be the one shown regardless of flight.
To fix this, you'll need to fetch the pilot info that is associated with the $flight
object. I would imagine that your $flight
object or one of the service singletons has a way to fetch pilot info for a specific flight.
Here is a formatted version of your code with stub classes that I created for testing. It'll be a good investment to create readable code in the future.. it is much easier to troubleshoot than long chains of one-line commands.
Output: http://i.imgur.com/b2nmxjJ.png
<?php
const PIREP_ACCEPTED = 1;
const PIREP_REJECTED = 2;
const PIREP_PENDING = 3;
const PIREP_INPROGRESS = 4;
const SITE_URL = 'http://www.example.com/';
class Flight {
public $phasedetail;
public $flightnum;
public $depname = 'depname' ;
public $arrname = 'arrname';
public $aircraftname = 'aircraftname';
public function __construct() {
$this->phasedetail = array_rand(array_flip(array('Boarding', 'Arrived', 'On Approach')));
$this->flightnum = rand(1000, 2000);
$this->aircraftname .= ' ' . $this->flightnum;
}
}
class PilotInfo {
public $pilotid;
public $code = 1;
public $firstname;
public $lastname;
public function __construct($id) {
$this->pilotid = $id;
$this->firstname = array_rand(array_flip(array('Jim', 'Jack', 'Joe')));
$this->lastname = array_rand(array_flip(array('Bob', 'Jackson', 'Doe')));
}
}
class Pirep {
public $code = 1;
public $pilotid;
public $pirepid;
public $accepted;
public $arricao;
public $aircraft;
public $flightnum;
public $flighttime;
public $landingrate;
public $depicao;
public $registration = 6;
public function __construct() {
$this->pilotid = rand(1, 100);
$this->pirepid = rand(1, 100);
$this->accepted = rand(0, 1);
}
}
class ACARSData {
public static function GetACARSData() {
return array(new Flight(), new Flight(), new Flight());
}
}
class OperationsData {
public static function getAircraftByReg ($registration) {
return rand(1, 100);
}
}
class PIREPData {
private static $reports = array();
public static function getRecentReportsByCount($count) {
if (empty(self::$reports)) {
for ($i = 0; $i < $count; ++$i) {
self::$reports[] = new Pirep();
}
}
return self::$reports;
}
}
class PilotData {
private static $cache = array();
public static function getPilotData($pirep_pilotid) {
if (empty(self::$cache[$pirep_pilotid])) {
self::$cache[$pirep_pilotid] = new PilotInfo($pirep_pilotid);
}
return self::$cache[$pirep_pilotid];
}
public static function getPilotCode($pilotinfo_code, $pilotinfo_pilotid) {
return $pilotinfo_pilotid;
}
}
class MainController {
public static function run($something, $something, $something) {
echo "some stats table";
}
}
?>
<!-- Start of live flights table -->
<div class="col-md-12 page-content">
<h2>Live Flights</h2>
<div class="stats-table">
<table>
<tr>
<th>Pilot</th>
<th>Flight</th>
<th>Departure</th>
<th>Arrival</th>
<th>Aircraft</th>
<th>Status</th>
</tr>
<?php
$results = ACARSData::GetACARSData();
if (count($results)>0) {
foreach($results as $flight) {
?><tr>
<?php
$count = 10;
$pireps = PIREPData::getRecentReportsByCount($count);
if($flight->phasedetail == "Boarding") {
echo "<img style='padding-left:3px;' src=''>";
} elseif($flight->phasedetail == "Arrived") {
echo "<img style='padding-left:3px;' src=''>";
} elseif($flight->phasedetail == "On Approach") {
echo "<img style='padding-left:3px;' src=''>";
}
foreach ($pireps as $pirep) {
$pilotinfo = PilotData::getPilotData($pirep->pilotid);
/* this is an unused variable! */
$pilotid = PilotData::getPilotCode($pilotinfo->code, $pilotinfo->pilotid);
}
?>
<td><?php echo '<a href="'.SITE_URL. '/index.php/profile/view/'.$pilotinfo->pilotid.'">'.$pilotinfo->firstname.' '.$pilotinfo->lastname.'</a>';?></td>
<td><?php echo $flight->flightnum;?></td>
<td><?php echo $flight->depname;?></td>
<td><?php echo $flight->arrname;?></td>
<td><?php echo $flight->aircraftname;?></td>
<td><?php if($flight->phasedetail != 'Paused') { echo $flight->phasedetail; } else { echo "Cruise"; }?></font></td>
</tr>
<?php
}
} else {
?><tr>
<td width="20%" align="center" colspan="6" style="padding: 5px; font-size: 13px; font-weight: bold; color: #3399FF;">No Flights in Progress!</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<!-- Start of recent flights table -->
<div class="col-md-12 page-content">
<h2>Recent Flights</h2>
<div class="stats-table">
<table>
<tr>
<th>Flight</th>
<th>Pilot</th>
<th>Departure</th>
<th>Arrival</th>
<th>Aircraft</th>
<th>Duration</th>
<th>V/S</th>
<th>Info</th>
</tr>
<?php
$count = 10;
$pireps = PIREPData::getRecentReportsByCount($count);
if (count($results)>0); // this line needs to be removed
if (count($pireps) > 0) {
foreach ($results as $flight); // this line needs to be removed
foreach ($pireps as $pirep) {
$pilotinfo = PilotData::getPilotData($pirep->pilotid);
$pilotid = PilotData::getPilotCode($pilotinfo->code, $pilotinfo->pilotid);
$acrid = OperationsData::getAircraftByReg($pirep->registration);
$results = ACARSData::GetACARSData();
$fcode = substr($flight->flightnum, 0, 3);
echo '<tr>';
echo '<td><a href="'.SITE_URL.'/index.php/pireps/viewreport/'.$pirep->pirepid.'">'.$pirep->code.$pirep->flightnum.'</a></td>';
echo '<td><a href="'.SITE_URL.'/index.php/profile/view/'.$pilotinfo->pilotid.'">'.$pilotinfo->firstname.' '.$pilotinfo->lastname.'</a></td>';
echo '<td>'.$pirep->depicao.'</td>';
echo '<td>'.$pirep->arricao.'</td>';
echo '<td>'.$pirep->aircraft.'</td>';
echo '<td>'.$pirep->flighttime.'</td>';
echo '<td>'.$pirep->landingrate.' ft/m</td>';
if ($pirep->accepted == PIREP_ACCEPTED) {
echo '<td><span class="label label-important"><font color="green">Accepted</font></span></td>';
} elseif($pirep->accepted == PIREP_REJECTED) {
echo '<td><span class="label label-important"><font color="red">Rejected</font></span></td>';
} elseif($pirep->accepted == PIREP_PENDING) {
echo '<td><span class="label label-warning"><font color="orange">Pending</font></span></td>';
} elseif($pirep->accepted == PIREP_INPROGRESS) {
echo '<td>On Progress</td>';
echo '</tr>';
}
}
} else {
echo '<tr><td>There are no recent Flights!</td></tr>';
}
?>
</table>
</div>
<!-- End of recent flights table -->
</div>
<!-- Start of booked flights table -->
<div class="col-md-12 page-content">
<h2>Booked Flights</h2>
<div class="stats-table">
<?php MainController::Run( 'FrontBids', 'RecentFrontPage', 10); ?>
</div>
</div>