php foreach循环跳过第一条记录

I've got a foreach loop that is skipping the first retrieved record on its first loop through data. I've got four years worth of data in my table. Each year has a color:

2011 - #0000FF 2012 - #ff33ff 2013 - #6666CC 2014 - #FFFF00

When I loop through it, the first year's color (regardless of which year I choose, since the data is dynamic) is blank. The second year has the first year's color, the third year has the second year's color, and so on.

Code looks like this:

while ($record = mysql_fetch_object($result2)) {
$set[$record->TransYear][] = $record;
}

function render($record) {
$output .= "<point lat='{$record->TransLat}' lng='{$record->TransLong}' 
poly='{$record->TransYear}' />";  
return $output;
}


foreach ($set as $TransYear => $records) {
$YearColor = $record->YearColor; 
print "<line category='{$TransYear}' color='{$YearColor}' width='4'>";
print $YearColor;
foreach ($records as $record) {
print render($record); 
}
print '</line>';
}

When I set my year query as 2011 and 2012, the output looks like this:

<line category="2011" color="" width="4">
<point lat="43.279" lng="-91.780" poly="2011"/>
<point lat="43.274" lng="-91.785" poly="2011"/>
<point lat="43.272" lng="-91.777" poly="2011"/>
<point lat="43.273" lng="-91.784" poly="2011"/>
<point lat="43.273" lng="-91.782" poly="2011"/>
<point lat="43.277" lng="-91.780" poly="2011"/>
<point lat="43.275" lng="-91.780" poly="2011"/>
<point lat="43.274" lng="-91.786" poly="2011"/>
<point lat="43.275" lng="-91.781" poly="2011"/>
<point lat="43.275" lng="-91.783" poly="2011"/>
<point lat="43.275" lng="-91.779" poly="2011"/>
</line>
<line category="2012" color="#0000FF" width="4">
<point lat="43.225" lng="-91.847" poly="2012"/>
<point lat="43.116" lng="-91.900" poly="2012"/>
<point lat="43.296" lng="-92.026" poly="2012"/>
<point lat="43.004" lng="-91.972" poly="2012"/>
<point lat="43.007" lng="-91.962" poly="2012"/>
<point lat="43.011" lng="-91.930" poly="2012"/>
<point lat="42.798" lng="-91.734" poly="2012"/>
</line>

In your first foreach loop you are referencing $record before it is defined in the inner foreach. You meant to use $records:

while ($record = mysql_fetch_object($result2)) {
  $set[$record->TransYear][] = $record;
}

function render($record) {
  $output .= "<point lat='{$record->TransLat}' lng='{$record->TransLong}' 
  poly='{$record->TransYear}' />";  
  return $output;
}

foreach ($set as $TransYear => $records) {
  $YearColor = $records->YearColor; // Here, change to $records
  print "<line category='{$TransYear}' color='{$YearColor}' width='4'>";
  print $YearColor;

  foreach ($records as $record) {
    print render($record); 
  }
  print '</line>';
}

By the way, you will probably get a lot of comments about not using PDO or mysqli instead of the deprecated mysql. I suggest looking into changing that. Also, you might consider not re-using variable names or close names in your various loops to avoid similar errors.

The second line below has $record, but the first line creates $records. If YearColor is not a variable in $TransYear then you need to get it from $records[0]

foreach ($set as $TransYear => $records) {
    $YearColor = $record->YearColor; 
    print "<line category='{$TransYear}' color='{$YearColor}' width='4'>";