HTML 数据未按我想要的方式显示,(PHP 数组)


HTML Data not displaying as I want, (PHP array)

我把我所有的数据存储在数组中

          <div class="box">
                            <div class="box-header">
                                 <h3 class="box-title">History Page Station Number:        <? php if($_POST){ $id; }?> </h3>
                            </div><!-- /.box-header -->
                            <div class="box-body table-responsive">
                                <table id="example2" class="table table-bordered table- hover">
                                    <thead>
                                        <tr>
                                           <th> Station Name</td>
    <th> Country </th>
    <th> Date </th>
    <th> Timestamp </th>
    <th> Temperature(celcius) </th>
    <th> Rainfall(mm)</th>
    <th> Windspeed(km/h)</th>
                                        </tr>
                                    </thead>
                                    <tbody>

    <!-- loop displaying all data -->
    <?php
    if($_POST){
    $cols = 1;
    for ($i=1; $i < count($wind); $i++)
    {
    echo "<tr>";
    for ($c=0; $c<$cols; $c++)
    {
  echo "<td>".$station->name."</td>";
  echo "<td>".$station->country."</td>";
  echo "<td>".$date[$i]."</td>";
  echo "<td>".$time[$i]."</td>";
  echo "<td>".$temp[$i]."</td>";
  echo "<td>".$rain[$i]."</td>";
  echo "<td>".$wind[$i]."</td>";
}
echo "</tr>";
}
}
?>
</tbody>
                                    <tfoot>
                                        <tr>
                                            <th> Station Name</td>
<th> Country </th>
<th> Date </th>
<th> Timestamp </th>
<th> Temperature(celcius) </th>
<th> Rainfall(mm)</th>
<th> Windspeed(km/h)</th>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div><!-- /.box-body -->
                        </div><!-- /.box -->

当我检查输出时,它在第一行中的显示元素[0]

Element[0] in 2nd row
Element[1] in 3rd
Element[0] in 4
Element[1] in 5
Element[2] in 6 etc

它再次从第一个元素开始并添加 1.。我不知道我做错了什么。

我的数组中有 22 个不同的元素

不确定为什么在 oner 中声明所有 col 时要执行列循环。即您不会在任何地方引用$c。设置 $i=0;最初也是如此。

即for ($i=0; $i

我试图理解你的逻辑,我举一个例子:

您可以使用每个:

    $weather['name'] = "Local Weather"; //ADD YOUR ROWS AS ARRAY
    $weather['country'] = "Italy"; //ADD YOUR ROWS AS ARRAY
    $weather['date'] = "2014-11-13"; //ADD YOUR ROWS AS ARRAY
    $weather['time'] = "15:20:32"; //ADD YOUR ROWS AS ARRAY
    $weather['temp'] = "18 &deg;C"; //ADD YOUR ROWS AS ARRAY
    $weather['rain'] = "32 mm"; //ADD YOUR ROWS AS ARRAY
    $weather['wind'] = "14 km/h"; //ADD YOUR ROWS AS ARRAY
    foreach ($weather as $val){
        echo "<tr>";
             echo "<td>".$weather['name']."</td>";
             echo "<td>".$weather['country']."</td>";
             echo "<td>".$weather['date']."</td>";
             echo "<td>".$weather['temp']."</td>";
             echo "<td>".$weather['rain']."</td>";
             echo "<td>".$weather['wind']."</td>";
        echo "</tr>";
    }