php - How to generate a foreach grid table -


i have code trying make grid table. have far generates results in 1 column. how can make when first column reaches 10 rows new column created?

<table> <thead> <tr> <th>players</th> </tr> </thead> <tbody>     <?php if( ( $players = $query->getplayers( ) ) !== false ): ?>     <?php foreach( $players $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> 

in case mentioned, need gather of data before printing table, build array , mathematics. due how table , it's children modeled. recommend switching columns first, rows if possible - mentioned in comments.

in recommended case, follows:

$i = 1; // start first row echo "<tr>";  foreach($players $player){      echo "<td>" . $player . "</td>";      // need new row?     if($i % max_column_number === 0){         echo "</tr><tr>";     }      // increase counter     $i++;  }  // end final row echo "</tr>"; 

if replace max_column_number 3 , have, example, 10 items in $players variable, this:

╔════╤═══╤═══╗ ║ 1  │ 2 │ 3 ║ ╠════╪═══╪═══╣ ║ 4  │ 5 │ 6 ║ ╟────┼───┼───╢ ║ 7  │ 8 │ 9 ║ ╟────┼───┼───╢ ║ 10 │   │   ║ ╚════╧═══╧═══╝ 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -