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 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了