【问题标题】:PHP code causing results to duplicatePHP代码导致结果重复
【发布时间】:2015-06-01 11:05:52
【问题描述】:

我已经创建了这个 PHP 脚本,它是从其他人的 sn-ps 中提取的,并且大部分情况下它都可以工作。但是,“实时航班”表发生了一些奇怪的事情,我似乎无法修复它,想知道这里是否有人可以提供帮助。

问题在于“试点”详细信息。我有一段代码应该为每个飞行员 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>

【问题讨论】:

  • 在您的示例页面中,没有实时航班详细信息,因此问题并不明显。作为一个附带问题,如果可能的话,我真的会尽量避免使用内联 CSS。
  • Php 肯定会延续糟糕的编程/艰难的调试实践。不是对这个程序员的评论,而是对 php 结构的反映
  • 这是什么意思:?php $count=1 0;
  • @Raad - 该页面正在获取实时数据,因此不幸的是,只有在多人飞行时(通常是英国晚上时间)才能看到实际错误。内联 CSS 是 PHPVMS 预构建内容管理系统的一部分。我不需要它用于 SEO 目的,所以一点内联 CSS 不是主要问题。 :)
  • @AsConfused - 我不直接知道那段代码是什么,但我会看看是否能找到。 :)

标签: php mysql


【解决方案1】:

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

没有使用来自$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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-11
    • 2023-03-22
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多