【问题标题】:Create sub-headers for dynamically created table headers in html在 html 中为动态创建的表头创建子表头
【发布时间】:2018-05-31 10:08:38
【问题描述】:

我正在尝试根据数据库值创建一个表,其中几乎没有动态创建的列。直到这里事情进展顺利。现在只针对动态表标题,我想为在列下生成的每个标题创建两个子标题,以显示两个不同的字段。这怎么可能实现。到目前为止,我正在尝试的还不够,需要一些见解。因此,请查看我的代码,其中“模块”是根据数据库中的模块数量动态生成的列,并且在每个“模块”列下我想放置“尝试”和“结果”子标题。任何帮助/见解都会非常有帮助。

    <table id="example" class="table table-striped table-hover table-bordered dataTableReport" cellspacing="0" width="100%">
    <thead>
    <tr>
    <th rowspan="2">S.No.</th>
    <th rowspan="2">E-mail ID</th> 
    <th rowspan="2">Name</th>
                <?php 
                     $query_select = "SELECT id from tbl;";
                     $result_select = mysql_query($query_select) or die(mysql_error());
                     $rows = array();
                      while($row = mysql_fetch_array($result_select))
                          $rows[] = $row;
                    foreach($rows as $row){ 
                          $mid = $row['id'];
                    echo "<th style='width:5%' colspan='2'>Module ".$mid."</th><tr><th>Attempt</th><th>Result</th>";
                     } ?>
   </tr>
   </thead>
   <tbody>

【问题讨论】:

    标签: php html html-table


    【解决方案1】:
    <?php
    $query = "SELECT id FROM tbl;";
    $result = mysqli_query($conn, $query);
    
    while($row = mysqli_fetch_assoc($result)){ 
    ?>
        <th style='width:5%' colspan='2'>Module <?php echo $row['id']; ?></th>
        <br><tr><th>Attempt</th><br><th>Result</th></tr>
    <?php
    }                                                               
    ?>
    

    我已将您的代码从 mysql 更新为 mysqli,主要是因为它过时并且没有所有更新的功能。我会开始学习 mysqli 以跟上竞争,并在以后阻止错误。

    $conn = 你应该把它重命名为你的数据库变量。

    【讨论】:

    • 感谢您抽出宝贵时间。但恐怕这并不能解决问题。现在我的桌子很奇怪。还能做点别的吗??
    • 能不能看到这个结果呢^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 2016-07-28
    • 2011-08-28
    • 2014-02-07
    • 2011-07-06
    • 2011-05-20
    相关资源
    最近更新 更多