dongmu5596 2010-07-19 12:30
浏览 22
已采纳

使用PHP Logic的动态表格布局

I have simple table that has about 80 rows, which I populate dynamically using PHP. What I am trying to do is to layout those rows in chunks for each column. So if I have 80 rows, I would like 4 columns of 20 rows or so, maybe the last column has less or more depending on the total number of rows. The total number of rows can change!

I am having trouble coming up with an implementation method that will not get messy! Anyone know of a simple way that I can implement this.

I have tried using a counter as I loop the data to populate the table and when a multiple of of 20 is reached move to the next block but that didn't work for me as I had extra rows left over.

foreach($indexes as $index){

    $counter++;

    echo '<tr>';

    if($counter > 20){

        $multiplier = $counter / 20;

        $head = '<td></td>';

        for($i=1; $i<$multiplier; $i++){

            $head .= '<td></td>';

        }

    }

    if($counter < 20){

        $head = ''; 

    }

    echo "$head<td>$index</td><td><input id='$index' name='$index' type='checkbox' /></td>";  

    echo '</tr>';

}

Thanks all for any help

  • 写回答

3条回答 默认 最新

  • dsfgds4215 2010-07-19 12:47
    关注

    I would do :

    $nbCols = 4;
    $nbRows = count($indexes)/$nbCols;
    for($row=0; $row<$nbRows; $row++) {
        echo "<tr>";
        for($i=0; $i<$nbCols; $i++) {
            $index = $indexes[$row + ($i*$nbRows)];
            echo "<td>$index</td><td><input id='$index' name='$index' type='checkbox' /></td>";
        }
        echo "</tr>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?