0

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

9
  • What exactly do you expect from first example? Commented Apr 19, 2014 at 3:16
  • @IvanNevostruev which example? Commented Apr 19, 2014 at 3:18
  • In your 1st example the $x is reset on each iteration of $i as it calls for ($x= 0; Commented Apr 19, 2014 at 3:18
  • @IvanNevostruev the simple one at the top? What I have below it Commented Apr 19, 2014 at 3:19
  • "So then why does this nested for loop not work?" My question it what exactly you think is wrong here? Commented Apr 19, 2014 at 3:21

1 Answer 1

2

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.