The result of this code is the first data in the table, following the table.

<style> .table { width: 100%; margin-bottom: 20px; } table { max-width: 100%; background-color: transparent; border-collapse: collapse; border-spacing: 0; } tbody { display: table-row-group; vertical-align: middle; border-color: inherit; } tr { display: table-row; vertical-align: inherit; border-color: inherit; } .table th, .table td { padding: 8px; line-height: 20px; text-align: left; vertical-align: top; border-top: 1px solid #dddddd; } user agent stylesheet td, th { display: table-cell; vertical-align: inherit; } td{width:150px;} </style> 

Somewhere there is an error in the code, and here is where.

  <table class="table"><tbody><tr><td>Рейсы</td><td>Отправление</td><td>Прибытие</td><td>Дни курсирования</td></tr><tr> <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"https://api.rasp.yandex.net/v1.0/schedule/?apikey=*******&format=json&uid=038AA_tis&station=s9623404&lang=ru&thread=false"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, FALSE); $response = curl_exec($ch); curl_close($ch); $json=json_decode($response,true); ?> <?php foreach ($json['schedule'] as $schedule): ?> <td><?= $schedule['thread']['short_title']; ?></td> <td><?= $schedule['departure_time']; ?></td><td> <?= $schedule['arrival_time'] ? $schedule['arrival_time'] : 'Не указано'; ?></td> <td> <?= $schedule['days']; ?></td></tr> </tbody></table> <?php endforeach; ?> 
  • It does not work, all the same - Arcadiy
  • So you decide, you have PHP incorrectly form the document? Then throw out the CSS from the question and add what exactly PHP is forming as a result and what is wrong with that. Or do you have a CSS problem? Then throw PHP nafig and replace static plate for a couple of lines. And rewrite the question tags. - Shallow

3 answers 3

 <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"https://api.rasp.yandex.net/v1.0/schedule/?apikey=51c9c870-925a-4058-9f23-613e5b74c93b&format=json&uid=038AA_tis&station=s9623404&lang=ru&thread=false"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, FALSE); $response = curl_exec($ch); curl_close($ch); $json = json_decode($response,true); ?> <table class="table"><tbody><tr><td>Рейсы</td><td>Отправление</td><td>Прибытие</td><td>Дни курсирования</td></tr> <?php foreach ($json['schedule'] as $schedule): ?> <tr> <td><?= $schedule['thread']['short_title']; ?></td> <td><?= $schedule['departure_time']; ?></td><td> <?= $schedule['arrival_time'] ? $schedule['arrival_time'] : 'Не указано'; ?></td> <td><?= $schedule['days']; ?></td> </tr> <?php endforeach; ?> </tbody></table> 
  • Do not mix mapping and calculations. You will get entangled in elementary things. - check1st

Move the opening <tr> inside the loop.

And closing </tbody></table> - out.

  • It does not work, all the same - Arcadiy

Because, at you each table table closes </tbody></table> ; only the markup of the end of the line </tr> is entered every iteration. And rediscover the tags, just not to write echo - seriously? Are these islands more readable than the format below? With highlighting variables, and so on:

 $out .= "<tr> <td>{$somevar[someprop]}</td> <td>{$somevar[nextprop]}</td> <td>{$somevar[finally]}</td> </tr>"; 

And besides:

 ?>Какова задача этого пробела? <?php