HTML数据没有按我的要求显示,(PHP数组)

时间:2014-11-13 16:19:46

标签: php html arrays html-table

我将所有数据存储在数组的

          <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个不同的元素

2 个答案:

答案 0 :(得分:0)

当您在oner中声明所有cols时,不确定为什么要进行列循环。 即你没有在任何地方提到$ c。 最初设置 $ i = 0;

for($ i = 0; $ i&lt; count($ wind); $ i ++)

答案 1 :(得分:0)

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

你可以使用foreach:

    $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>";
    }