duannei0044 2015-02-13 22:06
浏览 24

用数据库结果填充html表

I have to build html table that will look like this: enter image description here

But the fields and head names I get from database. First query - returns fields description, second query reurns fields values. I spent a lot hours trying to reach this. So what I do:

<table>
<tbody>
<?php
foreach ($comments as $comment) {   // $comments is array of strings                      
?>
     <td><b><?= $comment ?></b></td>
<?php}?>
</tbody>  
<?php
   foreach ($field_names as $fn) { // $field_names is array_keys($result)
   ?><tr><?php
     foreach ($result as $r) {  // $result - values of fields                          
     ?>
     <td><?= $r[$fn] ?></td>
     <?php
     }?>
</tr>
<?php
}
?></table>

But in this way I'm getting my content transposed. Any help, thanks.

  • 写回答

1条回答 默认 最新

  • duan010167787 2015-02-13 22:27
    关注

    ok, since you didnt provide the variable information, i will assume the following, and if so, this code should work for you:

    <?php
        $comments = ["comm1", "comm2", "comm3"]; // $comments is array of strings
    
        $field_names = array(
            0 => "fv1",
            1 => "fv2",
            2 => "fv3"
        ); // $field_names is array_keys($result)
    
        $result = ["value1", "val2", "val3"]// $result - values of fields
        ?>
        <table>
            <thead>
                <tr>
                    <?php foreach ($comments as $comment) { ?>
                        <th><b><?= $comment ?></b></th>
                    <?php } ?>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <?php foreach ($field_names as $k => $v) { ?>
                        <td><?= $result[$k] ?></td>
                    <?php } ?>
                </tr>
            </tbody>
        </table>
    

    UPDATE:

    <style>
        .tbl_hdr{
            font-weight: bold;
        }
    </style>
    <?php
    $comments = ["comm1", "comm2", "comm3"]; // $comments is array of strings
    $field_names = array(
       0 => "fv1",
       1 => "fv2",
       2 => "fv3"
    ); // $field_names is array_keys($result)
    
    $result = ["value1", "val2", "val3"]// $result - values of fields
    ?>
    <table>
        <?php foreach ($field_names as $k => $v) { ?>
            <tr>
                <td class="tbl_hdr"><?= $comments[$k] ?></td>
                <td><?= $result[$k] ?></td>
            </tr>
        <?php } ?>
    </table>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端