duanchuaiwan0063 2019-02-11 17:12
浏览 71
已采纳

如何将PHP循环拆分为两部分?

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

2条回答 默认 最新

  • douhuo3696 2019-02-12 16:27
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考