【问题标题】:Dynamic Table Layout using PHP Logic使用 PHP 逻辑的动态表格布局
【发布时间】:2010-07-19 20:47:41
【问题描述】:

我有大约 80 行的简单表,我使用 PHP 动态填充这些表。我想要做的是为每一列按块布局这些行。所以如果我有 80 行,我想要 20 行左右的 4 列,也许最后一列有更少或更多,这取决于总行数。总行数可以改变!

我无法想出一个不会弄乱的实现方法!任何人都知道我可以实现这一点的简单方法。

我尝试在循环数据以填充表格时使用计数器,当达到 20 的倍数时移动到下一个块,但这对我不起作用,因为我还有多余的行。

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>';

}

感谢大家的帮助

【问题讨论】:

    标签: php html css


    【解决方案1】:

    我愿意:

    $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】:

      你不想看看你部门的其余部分并处理它吗?

      if($counter % 20 == 0){
        // You've no remainder
      }else{
        // Do another loop to output the odd rows
      }
      

      或者您可以% 2 == 0 看看它是否是偶数,然后将整个结果乘以 10。

      请务必查看ceil()floor(),以确保您的行数是整数。

      【讨论】:

        【解决方案3】:

        如果你不介意这种单元格顺序:

        1 2 3 4
        5 6 7 8
        

        您可以在循环中使用&lt;div style='float:left'&gt;$cellValue&lt;/div&gt; 而无需使用表格。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-16
          • 2012-06-30
          • 2014-05-13
          • 2015-06-02
          • 1970-01-01
          • 2021-12-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多