【问题标题】:HTML Data not displaying as I want, (PHP array)HTML 数据未按我的意愿显示,(PHP 数组)
【发布时间】:2015-01-10 20:54:48
【问题描述】:

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

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

【问题讨论】:

  • 你有一个额外的空间:&lt;? php if($_POST){ $id; }?&gt; 首先删除它。
  • $station-&gt;namestation-&gt;country 来自哪里?
  • 删除整个东西,这不会什么都不做,或者在id中添加echo
  • 发布的代码没有明显问题。可能是您之前错误地填充了数组...
  • @cuSK 来自我的控制器。连接数据库并获取国家和电台名称

标签: php html arrays html-table


【解决方案1】:

当您在一个中声明所有列时,不确定为什么要进行列循环。 即你没有在任何地方引用 $c 。 初始也设置 $i=0;

for ($i=0; $i

【讨论】:

  • $cols 在这里是 1,内部循环什么都不做 :)
  • 好吧,几乎没有。
  • 感谢所有反馈,我会努力解决的
  • 如何填充 $date、$time、$temp 等。
【解决方案2】:

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

你可以使用 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>";
    }

【讨论】:

  • @rafael 你能发布你想要做什么吗?
  • 确定要在teamviewer上看到吗? 743 726 339 / 6776 我现在被困了大约 4 个小时:/
猜你喜欢
  • 2022-07-29
  • 1970-01-01
  • 2016-08-20
  • 2018-11-29
  • 1970-01-01
  • 2012-09-19
  • 2021-12-29
  • 2021-04-11
  • 1970-01-01
相关资源
最近更新 更多