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 │   │   ║
    ╚════╧═══╧═══╝
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大