简体   繁体   中英

How to split a PHP loop in 2 parts?

I have a weather json from where I get a wether forecast for 8 days. My problem is that I want to show the result in a table of 2 rows of 4 days each.
I managed to do it but in a very odd way that was pure begginer's luck during a trial-error-trial attempt :-) I don't even understand quite well why it is splitting the result in 2 rows... but it works!!! The only thing I could not do was to include a "hr" line between the 2 rows, to make the table easier to read.
You can see the result here http://www.meteocaldas.com/previsao_ds.php
With current code I am displaying each day forecasted values inside the same "td" in different lines using "br". I have been reading that it is not correct to use "br" inside "td" so I am not sure that I am doing the righ thing.
Wouldn't it be better to use a table with 4 columns (one for each day) and have different rows for each of the values? Is there any way to rewrite this code to make it more efficient and look less "childish"? :-) Thanks in advance!

<?php
     (...)
 $decoded = json_decode($rawData, true); 
?>
 <table>
<?php for($k=0;$k<8;$k++){ 
$dailyvalue = $decoded['daily']['data'][$k];

    $dailyTime = $dailyvalue['time'];
$dailyIcon = $dailyvalue['icon'];
$dailyTempMax = round($dailyvalue['temperatureMax'],0); 
$dailyTempMin = round($dailyvalue['temperatureMin'],0);
      (...)
 ?> 

  <!-- table for 8 day's forecast (2 rows/4 days each) -->
<td>
<?php echo strftime("%a %d",$dailyTime) ?>
 <br>
<?php echo '<img src="path/'.$dailyIcon.'.png">' ?>
 <br>
<?php echo $dailyTempMin.'º' ?> </span>
 <br>
 <?php echo $dailyTempMax.'º' ?></span>
  <br>
        (...)   

 <?php if ($k == 3) {
    echo '</td></tr>';    
  } ?>

 <?php
}
 ?>
</td></tr><table>

为什么不简单地将两个不同的表彼此堆叠呢?

As per your comment-request, here's some sample-pseudo code, based on your code above:

<?php
    (...)
$decoded = json_decode($rawData, true); 
for($k=0;$k<8;$k++){ 
    $dailyvalue = $decoded['daily']['data'][$k];

    $dailyTime[$k] = $dailyvalue['time'];
    $dailyIcon[$k] = $dailyvalue['icon'];
    $dailyTempMax[$k] = round($dailyvalue['temperatureMax'],0); 
    $dailyTempMin[$k] = round($dailyvalue['temperatureMin'],0);
}

?>
<table>
<?php
echo "<tr>";
for($k = 0; $k <= 3; $k++){
    echo "<td>".strftime("%a %d",$dailyTime[$k])."</td>"; // this will make the "datetime" row
}
echo "</tr><tr>";
for($k = 0; $k <= 3; $k++){
    echo "<td><img src=path/".$dailyIcon[$k].".png></td>"; // this will make the "icon" row
}
echo "</tr><tr>";
for($k = 0; $k <= 3; $k++){
    echo "<td>".$dailyTempMin[$k]."º</td>"; // this will make the "MinTemp" row
}
echo "</tr><tr>";
for($k = 0; $k <= 3; $k++){
    echo "<td>".$dailyTempMax[$k]."º</td>"; // this will make the "MaxTemp" row
}
echo "</tr>";
// put stuff you want between the tables here
echo "<tr>";
for($k = 4; $k <= 7; $k++){
     // proceed to do the same as above

Mind you, there are further ways you can reduce the screen clutter (like moving the table-drawing for loops into a function), but this is the general gist of it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM