douyan1970 2015-09-30 16:27
浏览 49
已采纳

如何生成foreach网格表

I have this code that I am trying to make into a grid table. What I have so far generates the results all in one column. How can I make it so that when the first column reaches 10 rows a new column is created?

<table>
<thead>
<tr>
<th>Players</th>
</tr>
</thead>
<tbody>
    <?php if( ( $Players = $Query->GetPlayers( ) ) !== false ): ?>
    <?php foreach( $Players as $Player ): ?>
<tr>
<td align=center><?php echo "<img src=https://crafatar.com/avatars/".$Player."/?helm&size=32>
              <p align=center>".$Player."</p>"; ?></td>
</tr>
    <?php endforeach; ?>
    <?php else: ?>
    <tr>
    <td>No players Online.</td>
    </tr>
    <?php endif; ?>
</tbody>
</table>
  • 写回答

1条回答 默认 最新

  • duangua6912 2015-09-30 17:53
    关注

    In the case you mentioned, you would need to gather all of the data before printing the table, build an array and do some mathematics. This is due to how the Table and it's children are Modeled. I would recommend switching to columns first, then rows if possible - like it was mentioned in the comments.

    In the recommended case, it would be as follows:

    $i = 1;
    // start first row
    echo "<tr>";
    
    foreach($Players as $Player){
    
        echo "<td>" . $Player . "</td>";
    
        // do we need a new row?
        if($i % MAX_COLUMN_NUMBER === 0){
            echo "</tr><tr>";
        }
    
        // increase counter
        $i++;
    
    }
    
    // end final row
    echo "</tr>";
    

    If you replace MAX_COLUMN_NUMBER with 3 and have, for example, 10 items in the $Players variable, it would look like this:

    ╔════╤═══╤═══╗
    ║ 1  │ 2 │ 3 ║
    ╠════╪═══╪═══╣
    ║ 4  │ 5 │ 6 ║
    ╟────┼───┼───╢
    ║ 7  │ 8 │ 9 ║
    ╟────┼───┼───╢
    ║ 10 │   │   ║
    ╚════╧═══╧═══╝
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站