title: PHP code causing results to duplicate
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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 <a href="http://www.virginatlanticvirtual.co.uk" rel="nofollow">www.virginatlanticvirtual.co.uk</a></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Thanks in advance.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!-- Start of live flights table --&gt;
  &lt;div class="col-md-12 page-content"&gt;
    &lt;h2&gt;Live Flights&lt;/h2&gt;
    &lt;div class="stats-table"&gt;
      &lt;table&gt;
        &lt;tr&gt;
          &lt;th&gt;Pilot&lt;/th&gt;
          &lt;th&gt;Flight&lt;/th&gt;
          &lt;th&gt;Departure&lt;/th&gt;
          &lt;th&gt;Arrival&lt;/th&gt;
          &lt;th&gt;Aircraft&lt;/th&gt;
          &lt;th&gt;Status&lt;/th&gt;
        &lt;/tr&gt;
        &lt;?php $results=A CARSData::GetACARSData(); if (count($results)&gt;0) { foreach($results as $flight) { ?&gt;
        &lt;tr&gt;
          &lt;?php $count=1 0; $pireps=P IREPData::getRecentReportsByCount($count); ?&gt;
          &lt;?php if($flight-&gt;phasedetail == "Boarding") { echo "
          &lt;img style='padding-left:3px;' src=''&gt;"; } elseif($flight-&gt;phasedetail == "Arrived") { echo "
          &lt;img style='padding-left:3px;' src=''&gt;"; } elseif($flight-&gt;phasedetail == "On Approach") { echo "
          &lt;img style='padding-left:3px;' src=''&gt;"; } ?&gt;
  
          &lt;?php foreach ($pireps as $pirep) { $pilotinfo=P ilotData::getPilotData($pirep-&gt;pilotid); $pilotid = PilotData::getPilotCode($pilotinfo-&gt;code, $pilotinfo-&gt;pilotid); } ?&gt;
          &lt;td&gt;
            &lt;?php echo '&lt;a href="'.SITE_URL. '/index.php/profile/view/'.$pilotinfo-&gt;pilotid.'"&gt;'.$pilotinfo-&gt;firstname.' '.$pilotinfo-&gt;lastname.'&lt;/a&gt;';?&gt;&lt;/td&gt;
          &lt;td&gt;
            &lt;?php echo $flight-&gt;flightnum;?&gt;&lt;/td&gt;
          &lt;td&gt;
            &lt;?php echo $flight-&gt;depname;?&gt;&lt;/td&gt;
          &lt;td&gt;
            &lt;?php echo $flight-&gt;arrname;?&gt;&lt;/td&gt;
          &lt;td&gt;
            &lt;?php echo $flight-&gt;aircraftname;?&gt;&lt;/td&gt;
          &lt;td&gt;
            &lt;?php if($flight-&gt;phasedetail != 'Paused') { echo $flight-&gt;phasedetail; } else { echo "Cruise"; }?&gt;&lt;/font&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
        &lt;?php } } else { ?&gt;
        &lt;tr&gt;
          &lt;td width="20%" align="center" colspan="6" style="padding: 5px; font-size: 13px; font-weight: bold; color: #3399FF;"&gt;No Flights in Progress!&lt;/td&gt;
        &lt;/tr&gt;
        &lt;?php } ?&gt;
      &lt;/table&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;!-- Start of recent flights table --&gt;
  &lt;div class="col-md-12 page-content"&gt;
    &lt;h2&gt;Recent Flights&lt;/h2&gt;
    &lt;div class="stats-table"&gt;
      &lt;table&gt;
        &lt;tr&gt;
          &lt;th&gt;Flight&lt;/th&gt;
          &lt;th&gt;Pilot&lt;/th&gt;
          &lt;th&gt;Departure&lt;/th&gt;
          &lt;th&gt;Arrival&lt;/th&gt;
          &lt;th&gt;Aircraft&lt;/th&gt;
          &lt;th&gt;Duration&lt;/th&gt;
          &lt;th&gt;V/S&lt;/th&gt;
          &lt;th&gt;Info&lt;/th&gt;
        &lt;/tr&gt;
        &lt;?php $count=1 0; $pireps=P IREPData::getRecentReportsByCount($count); ?&gt;
        &lt;?php if (count($results)&gt;0); if (count($pireps) &gt; 0) { foreach ($results as $flight); foreach ($pireps as $pirep) { $pilotinfo = PilotData::getPilotData($pirep-&gt;pilotid); $pilotid = PilotData::getPilotCode($pilotinfo-&gt;code, $pilotinfo-&gt;pilotid); $acrid = OperationsData::getAircraftByReg($pirep-&gt;registration);
        $results = ACARSData::GetACARSData(); $fcode = substr($flight-&gt;flightnum, 0, 3); echo '
        &lt;tr&gt;'; echo '
          &lt;td&gt;&lt;a href="'.SITE_URL.'/index.php/pireps/viewreport/'.$pirep-&gt;pirepid.'"&gt;'.$pirep-&gt;code.$pirep-&gt;flightnum.'&lt;/a&gt;
          &lt;/td&gt;'; echo '
          &lt;td&gt;&lt;a href="'.SITE_URL.'/index.php/profile/view/'.$pilotinfo-&gt;pilotid.'"&gt;'.$pilotinfo-&gt;firstname.' '.$pilotinfo-&gt;lastname.'&lt;/a&gt;
          &lt;/td&gt;'; echo '
          &lt;td&gt;'.$pirep-&gt;depicao.'&lt;/td&gt;'; echo '
          &lt;td&gt;'.$pirep-&gt;arricao.'&lt;/td&gt;'; echo '
          &lt;td&gt;'.$pirep-&gt;aircraft.'&lt;/td&gt;'; echo '
          &lt;td&gt;'.$pirep-&gt;flighttime.'&lt;/td&gt;'; echo '
          &lt;td&gt;'.$pirep-&gt;landingrate.' ft/m&lt;/td&gt;'; if($pirep-&gt;accepted == PIREP_ACCEPTED) echo '
          &lt;td&gt;&lt;span class="label label-important"&gt;&lt;font color="green"&gt;Accepted&lt;/font&gt;&lt;/span&gt;
          &lt;/td&gt;'; elseif($pirep-&gt;accepted == PIREP_REJECTED) echo '
          &lt;td&gt;&lt;span class="label label-important"&gt;&lt;font color="red"&gt;Rejected&lt;/font&gt;&lt;/span&gt;
          &lt;/td&gt;'; elseif($pirep-&gt;accepted == PIREP_PENDING) echo '
          &lt;td&gt;&lt;span class="label label-warning"&gt;&lt;font color="orange"&gt;Pending&lt;/font&gt;&lt;/span&gt;
          &lt;/td&gt;'; elseif($pirep-&gt;accepted == PIREP_INPROGRESS) echo '
          &lt;td&gt;On Progress&lt;/td&gt;'; echo '&lt;/tr&gt;'; } } else { echo '
        &lt;tr&gt;
          &lt;td&gt;There are no recent Flights!&lt;/td&gt;
        &lt;/tr&gt;'; } ?&gt;
      &lt;/table&gt;
    &lt;/div&gt;
    &lt;!-- End of recent flights table --&gt;
  &lt;/div&gt;
  &lt;!-- Start of booked flights table --&gt;
  &lt;div class="col-md-12 page-content"&gt;
    &lt;h2&gt;Booked Flights&lt;/h2&gt;
    &lt;div class="stats-table"&gt;
      &lt;?php MainController::Run( 'FrontBids', 'RecentFrontPage', 10); ?&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>