duanjiao5261 2013-03-24 04:09
浏览 31
已采纳

打印一个表,两列PHP关联数组作为三个并排的双列表

I use the following to print a 15-row, two-column table from an associative arrray in PHP:

<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
    <?
    $rowCount = 0;
    foreach($word_frequency as $word => $frequency) if ($rowCount++ < 15) {?>
        <tr>
            <td><? echo $word; ?></td>
            <td<? echo $frequency; ?></td>
        </tr>
    <?}?>
</tbody>
</table>

It works fine but takes up too much vertical space on the page. How can I format this into three side-by-side tables of five rows and two columns each with the first group having array items 1-5, the second group 6-10, and the last group 11-15? (refer to following illustration):

key1  value1  |  key6   value6   |  key11   value11
key2  value2  |  key7   value7   |  key12   value12
...
...
key5  value5  |  key10  value10  |  key15   value15 

I've tried various table, div, container, and multiple loop experiments with very mixed (and unsuitable) results. Thank you in advance.

  • 写回答

4条回答 默认 最新

  • doudao1837 2013-03-24 04:32
    关注

    Try some thing like that.

    <table width="100%">
    <tr>
    <?php
        $rowCount = 0;
        foreach($word_frequency as $word => $frequency) if ($rowCount < 15) { ?>
       <?php if($rowCount % 5 == 0) {  ?>
         <td><table border=1>
       <?php } ?>                   
           <tr>
             <td><?php echo $word; ?></td>
             <td><?php echo $frequency; ?></td>
           </tr>
           <?php if($rowCount % 5 == 4) {  ?>
         </table></td>
       <?php } ?>                   
    <?php $rowCount++; } ?>
    </tr>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题