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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c