MT-NL22
MT-NL22

Reputation: 35

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 ",
            "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>

Upvotes: 1

Views: 607

Answers (3)

Kamrul Khan
Kamrul Khan

Reputation: 3350

Try this:

          $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");


    echo '<table border=1 rows=5 cols=5><tr>';
    for($i =0; $i < 25; $i++){
        if ($i%5 === 0) {
            echo '</tr><tr>';
        }
        echo "<td>" . $games[$i] . "</td>";
    }
    echo '</tr></table>';

Upvotes: 0

Blinkydamo
Blinkydamo

Reputation: 1584

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>

Upvotes: 1

devpro
devpro

Reputation: 16117

In your code, you must need to use <table> outside the loop, than you must need to define 5 columns than you can use your $games array as you need:

Example:

<?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");
?>

<table border="1">
<tr>
<?php
// this will print 5 columns
for ($i=1; $i <= 5 ; $i++) { 
?>
<td><?php echo "Column". $i ?></td> 
<?php
}
?>
</tr> 
<?php
// this will print your games value in 5 rows each
$games = array_chunk($games, 5); // break in chunks
foreach ($games as $key => $value) {
    echo "<tr>"; // starting tr for values
    foreach ($value as $fvalue) { // this will break in 5 rows each.
    ?>
    <td><?=$fvalue?></td>
    <?php           
    }
    echo "</tr>"; // closing tr
}
?>
</table>

DEMO

Upvotes: 2

Related Questions