【问题标题】:Display array values in a table?在表格中显示数组值?
【发布时间】:2016-11-25 16:17:23
【问题描述】:

我正在研究数组,并试图弄清楚如何在表格中显示 my_array 的键和值。我希望有人能告诉我怎么做。这是我正在使用的代码。当我执行var_dump($my_data); 时,我可以看到所有数据,现在我只想了解如何显示它。谢谢。

<?php

$result_list= array();
$query="SELECT * from tbl_uploads";

//Get info from table
$result=mysqli_query($db_con,$query);
while ($row = mysqli_fetch_array($result)) {
  $result_list[] = $row;
}
foreach($result_list as $row) {

    $my_data[] = array(
        'user_id' => $row['user_id'],
        'user_name'  => $row['user_name'],
        'file'  => $row['file']
    );              
  }
var_dump($my_data);
?>

【问题讨论】:

标签: php


【解决方案1】:

这是一个简单的example。首先,获取数组键并将它们用作标题:

<table>
    <thead>
        <tr>
            <?php foreach (array_keys(reset($my_data)) as $heading) : ?>
                <th><?php echo $heading ?></th>
            <?php endforeach; ?>
        </tr>
    </thead>

接下来,循环内容并按行显示:

    <tbody>
        <?php foreach ($my_data as $row) : ?>
            <tr>
                <?php foreach ($row as $cell) : ?>
                    <td><?php echo $cell ?></td>
                <?php endforeach; ?>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

【讨论】:

  • 太棒了,令人印象深刻。谢谢。
猜你喜欢
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
  • 2014-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多