MT-NL22 MT-NL22 - 26 days ago 11
CSS Question

PHP how to sort output of the array in a 5x5 table?

I'm very new to PHP.
I am trying to get the game (names) in a 5x5 table with 5 rows and 5 cols.
But i want to generate the table by the number output of games and not a preset html table.
i made it work that it shows the games but not in a table.
Could you please help me and give me a good explaination so i understand too.
Thank's already!

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Merijn</title>
</head>
<body>
<?php
$games = array(
"battlefield 1",
"battlefield 2",
"Out of the Park Baseball 17",
"Overwatch",
"Stephen's Sausage Roll",
"Dark Souls III",
"Kentucky Route Zero - Act IV NEW",
"Stardew Valley",
"Ori and the Blind Forest",
"XCOM 2",
"World of Warcraft: Legion",
"Steins;Gate ",
"The Witness",
"Inside",
"Hex: Shards of Fate",
"Forza Horizon 3 ",enter code here
"Rise of the Tomb Raider",
"Total War: WARHAMMER",
"Chime Sharp",
"Pony Island",
"F1 2016",
"Day of the Tentacle Remastered",
"Tales from the Borderlands",
"DOOM",
"Hearthstone");
for($i =0; $i <=25; $i++){
echo '<table rows=5 cols=5>';
echo "<td>" . $games[$i] . "</td>" . "<br/>";
}
?>
</body>
</html>

Answer

Maybe not the cleanest but it works :)

<?php
$games = array(
            "battlefield 1",
            "battlefield 2",
            "Out of the Park Baseball 17",
            "Overwatch",
            "Stephen's Sausage Roll",
            "Dark Souls III",
            "Kentucky Route Zero - Act IV NEW",
            "Stardew Valley",
            "Ori and the Blind Forest",
            "XCOM 2",
            "World of Warcraft: Legion",
            "Steins;Gate ",
            "The Witness",
            "Inside",
            "Hex: Shards of Fate",
            "Forza Horizon 3 ",
            "Rise of the Tomb Raider",
            "Total War: WARHAMMER",
            "Chime Sharp",
            "Pony Island",
            "F1 2016",
            "Day of the Tentacle Remastered",
            "Tales from the Borderlands",
            "DOOM",
            "Hearthstone");
    $c = 0;
?>
<table>
<tr>
    <?php for ($i=0; $i < count( $games ); $i++) { 
        if( $c == 5 )
        {
          $c = 0;
            ?>
                </tr>
                <tr>

        <?php } echo "<td>".$games[ $i ]."</td>"; $c++;
    } ?>
    </tr>

</table>
Comments