PHP代码导致结果重复

时间:2015-06-01 11:05:52

标签: php mysql

我已经创建了这个PHP脚本,从其他人的片段中拉出来,并且大部分都是有效的。然而,“实时航班”表有一些奇怪的事情,我似乎无法解决它,并想知道这里是否有人可以提供帮助。

问题在于“飞行员”细节。我有一段代码,对于每个飞行员pirep,应该识别他们的ID,然后加载他们的名字和姓氏。这与第二张表(最近的航班)完全相同,而且该部分完美无缺。出于某种原因,在第一张桌子中,当只有一名飞行员飞行时,情况正常。一旦我得到两名飞行员,飞行表显示飞行员的名字都是相同的,并不一定是当时飞行员的名字。

我已粘贴下表。该编码构成了名为PHPVMS的虚拟航空管理系统的一部分,基于PHP,Dataclasses和MySQL。您可以在www.virginatlanticvirtual.co.uk

看到该页面

我目前的想法是,因为第一个和第二个飞行表使用相同的代码,这可能导致冲突,但我不够技术,无法解决如何停止冲突或更改一个代码所以它的完成方式不同。

这是我希望社区可以帮助我的地方。如果您还需要了解其他任何内容或问题,请告知我们。

提前致谢。

<!-- 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>

1 个答案:

答案 0 :(得分:0)

您的航班与飞行员之间存在着有缺陷的联系。

未使用$flight对象中的信息获取导航。它只是一遍又一遍地获取相同的静态列表,并且每次都选择最终元素。 ;)

$results的每次迭代中(您查看单个$flight),您可以从最近的报告(PIREPData::getRecentReportsByCount($count))请求前10个结果。该清单不会改变;每个$flight查看相同的10个报告。

此外,查看$pireps的循环始终会导致$pilotinfo等于$pireps的最终元素。无论飞行如何,第10名飞行员将始终显示。

要解决此问题,您需要获取与$flight对象关联的导频信息。我想你的$flight对象或其中一个服务单身人士可以获取特定航班的飞行员信息。

以下是我创建的用于测试的存根类的代码的格式化版本。将来创建可读代码将是一项很好的投资。比单行命令的长链更容易排除故障。

输出: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>