doubi1931 2014-09-12 23:13
浏览 282

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>
  • 写回答

2条回答 默认 最新

  • doujuan2688 2014-09-12 23:18
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害