dpg2905 2013-07-16 20:22
浏览 18
已采纳

动态构建<th>和<tr>

The amount of columns in a table is based upon the amount of data in a table. The amount of rows in a table is based on the amount of rows in a different table. An example of these tables are below

environments        patches
  id | name           id |    name
 ____|______         ____|____________
   1 | dev             1 | firstPatch
   2 | test            2 | secondPatch
   3 | prod            3 | thirdPatch

The end result of what I'm looking for would be the following <table>

 _________________________________
| Patch Name  | dev | test | prod |
|_____________|_____|______|______|
| thirdPatch  |     |      |      |
| secondPatch |  x  |      |      |
|_firstPatch__|_____|______|______|

I understand how I could use a while() loop to build the table headers. And I understand how I could use a while() to build the rows.

echo "<tr>";
while ($env = $listEnvs->fetch_assoc()){
    echo "<th>".$env['name']."</th>"
}
echo "</tr>";

--AND--

while ($patch = $listPatches->fetch_assoc()){
    echo "<tr><td>".$patch['name']."</td></tr>";
}

What I am struggling with is that the cells for each patch/environment pair are going to have data that is pulled from several other tables. So knowing the patch.id as well as the environment.id is important. Example the cell with the x is patch.id = 2, environment.id = 1 When constructing the table, how can I make sure each cell has this necessary information?

  • 写回答

1条回答 默认 最新

  • dongzenglin8292 2013-07-16 20:30
    关注

    Try to create an array like $content[$patch.id][$environment.id].

    Then you can add echo($content[$patch.id][$environment.id]) to your while of the $patch. You walk through the patches, so $patch.id is known. For $environment.id, you should make an array with all environment.id's and walk through it with $environment.id[i], where $i should be increased with each new column (and set to 0 when a new row is started).

    In (bad) pseudo-code:

    walkthrough environments {
    
        save array with environments
        echo table headers
    
    }
    
    walk through patches {
    
        echo table 'row headers'
    
        walk through environment array {
    
            echo content[patch.id][environment.id]
    
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路