douju7245 2014-04-19 03:08
浏览 101
已采纳

嵌套for循环不能正常工作

So when I have a nested for loop like such

<?php for ($i= 0; $i< 5; $i++)
{
    for ($x= 0; $x< 5; $x++)
    {
        echo $x;
    }

    echo "<br/>";
} ?>

it returns:

01234
01234
01234
01234
01234

So then why does the nested for loop below not work? Instead of starting each $x again, it just combines them together. So (assuming the results are the same as the above example) the first row will be 01234, but the second row will be 0123456789, with the 01234 being the same as the first row., and so on. So the 8th row will contain all the values from row 1 to 8.

<?php
try {
    $sql = "SELECT timetable.id as id, timetable.day as serviceday, timetable.station, timetable.departs as time, CURTIME(), 
CASE WHEN ROUND(TIME_TO_SEC(timetable.departs)/60 - TIME_TO_SEC(CURTIME())/60,0) > 120 THEN FLOOR(ROUND(TIME_TO_SEC(timetable.departs)/60 - TIME_TO_SEC(CURTIME())/60,0)/60) ELSE ROUND(TIME_TO_SEC(timetable.departs)/60 - TIME_TO_SEC(CURTIME())/60,0) END as departs, 
CASE WHEN ROUND(TIME_TO_SEC(timetable.departs)/60 - TIME_TO_SEC(CURTIME())/60,0) <= 120 THEN 'min' ELSE 'hrs' END as units, DATE_FORMAT(timetable.departs, '%l:%i %p') as time2, 
CASE WHEN timetable.platform != platformchange.platform THEN platformchange.platform ELSE timetable.platform END as platform,
 timetable.route, timetable.run, route.code, line.name, line.translink, station.name as terminus, line.colour, ns, a.departs as terminustime, IFNULL(c.id,-1) as cancel FROM timetable INNER JOIN route ON timetable.route = route.id INNER JOIN line ON route.line = line.id INNER JOIN station ON route.terminus = station.id INNER JOIN (SELECT departs, station, route, run FROM timetable) a ON a.station = station.id AND timetable.route = a.route AND timetable.run = a.run LEFT JOIN (SELECT service, platform FROM platformchange  WHERE day LIKE '%" . $day . "%') as platformchange ON timetable.id = platformchange.service LEFT JOIN overnight ON timetable.route = overnight.route AND timetable.run = overnight.run LEFT JOIN (SELECT cancel.id, cancel.route, cancel.run FROM cancel WHERE cancelday = CURDATE()) as c ON c.route = timetable.route AND c.run = timetable.run  WHERE timetable.station = " . $_GET['id'] . " AND timetable.departs > CURTIME() AND (timetable.day LIKE '%" . $day . "%' OR timetable.date = CURDATE()) AND IFNULL(c.id,-1) < 1 GROUP BY timetable.id ORDER BY time ASC LIMIT 12";
    $s = $pdo->prepare($sql);
    $s->execute();
}
catch (PDOException $e){

}

foreach($s as $row){
    $services[] = array('id' => $row['id'], 'time' => $row['time'], 'platform' => $row['platform'], 'terminus' => $row['terminus'], 'colour' => $row['colour'], 'departs' => $row['departs'] . ' ' . $row['units'], 'route' => $row['route'], 'run' => $row['run'], 'time2' => $row['time2'], 'ns' => $row['ns'], 'terminustime' => $row['terminustime'], 'tl' => $row['translink']);
}

for($i = 0; $i < count($services); $i++){
    try {
        $query = "SELECT station.name as station, station.distancefromcentral, ROUND(TIME_TO_SEC(departs)/60 - TIME_TO_SEC(CURTIME())/60,0) as orderby, CASE WHEN ROUND(TIME_TO_SEC(departs)/60 - TIME_TO_SEC(CURTIME())/60,0) > 120 THEN FLOOR(ROUND(TIME_TO_SEC(departs)/60 - TIME_TO_SEC(CURTIME())/60,0)/60) ELSE ROUND(TIME_TO_SEC(departs)/60 - TIME_TO_SEC(CURTIME())/60,0) END as departs, CASE WHEN ROUND(TIME_TO_SEC(departs)/60 - TIME_TO_SEC(CURTIME())/60,0) > 120 THEN 'hrs' ELSE 'min' END as units, timetable.route, timetable.run, day, date, station.distancefromcentral
FROM timetable 
INNER JOIN station ON timetable.station = station.id 
LEFT JOIN overnight ON overnight.route = timetable.route AND overnight.run = timetable.run
WHERE (departs > '" . $services[$i]['time'] . "' AND (day LIKE '%" . $day . "%' OR date LIKE CURDATE()) AND timetable.route = " . $services[$i]['route'] . " 
AND timetable.run = " . $services[$i]['run'] . ")
OR ( overnight.id > 0 AND(day LIKE '%" . $nextday . "%' OR date LIKE DATE_ADD(CURDATE(), INTERVAL 1 DAY) ) AND timetable.route = " . $services[$i]['route'] . " 
AND timetable.run = " . $services[$i]['run'] . ")  
ORDER BY date, 
  CASE
    WHEN DATE_FORMAT(CURDATE(),'%w') = 0
      THEN `day` 
    END DESC,
  CASE
    WHEN DATE_FORMAT(CURDATE(),'%w') <> 0
      THEN `day` 
    END ASC, 
  orderby ASC";
        $z = $pdo->prepare($query);
        $z->execute();
    }

    catch (PDOException $error){
        echo 'error';
    }

    foreach($z as $z){
        $stops[] = array('station' => $z['station'], 'departs' => $z['departs'] . ' ' . $z['units'], 'dist' => $z['distancefromcentral'], 'route' => $z['route'], 'run' => $z['run'], 'day' => $z['day'], 'orderby' => $z['orderby'], 'units' => $z['units']);
    }

    if(isset($stops)){                          
        for($x = 0; $x < count($stops); $x++){
            echo $x;
        }
    }
    echo '<br/>';
}
?>

So here is an example of just 2 rows

012345678910111213
012345678910111213141516

However the second row only has 3 values, so should only be 012. It's not separating them per $i, just combining them

If I'm not explaining it well I apologise

  • 写回答

1条回答 默认 最新

  • dsa88886666 2014-04-19 03:36
    关注

    I believe your problem is this part

    foreach($z as $z){
        $stops[] = array('station' => $z['station'], 'departs' => $z['departs'] . ' ' . $z['units'], 'dist' => $z['distancefromcentral'], 'route' => $z['route'], 'run' => $z['run'], 'day' => $z['day'], 'orderby' => $z['orderby'], 'units' => $z['units']);
    }
    

    On each loop you are adding to $stops, so if the 1st had 14, then the 2nd would add 3 more, so it would be 17 not just 3.

    You can either reset the array before

    $stops = array();
    foreach($z as $z){
     ...
    

    Or add the $i as a key

    foreach($z as $z){
        $stops[$i][] = array('station' => $z['station'], 'departs' => $z['departs'] . ' ' . $z['units'], 'dist' => $z['distancefromcentral'], 'route' => $z['route'], 'run' => $z['run'], 'day' => $z['day'], 'orderby' => $z['orderby'], 'units' => $z['units']);
    }
    if(isset($stops[$i])){                          
        for($x = 0; $x < count($stops[$i]); $x++){
            echo $x;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)