1

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>
4
  • u need to use table outside the loop, but still this will only print 1 column and multiple rows, and your length should be $i < 25 because you are starting this from 0 Commented Nov 9, 2016 at 15:16
  • On a good way to reach your target. @devpro comment shows you some mistakes you need to repair, and then you just need to add opening and closing of <tr> after each 5 of games. You can use $i to check if its modulo division by 5 gives you no reminder and that means its time to start new row. Commented Nov 9, 2016 at 15:21
  • @Ranker Sorry i dont get it. Could you maybe give me a tip or something? Commented Nov 9, 2016 at 15:27
  • he is saying you are using <table> inside the loop, which is wrong, @Ranker, now u have 2 solutions, try them Commented Nov 9, 2016 at 15:36

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Gotta love a demo too ;-) Sidenote: In order to get clean HTML, it would be best to add "\n" after each element. That way, HTML source won't reveal as one big clump on a single line, which makes it hard to debug if/when HTML source needs to be viewed.
1

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>

5 Comments

I'm getting 6 rows, not 5.
My bad, started the count at 1 instead of 0, fixed and updated
Could you explain me what this does i dont know the operator $i%5 === 0 for($i =0; $i < 25; $i++){ if ($i%5 === 0) { echo '</tr><tr>'; } echo "<td>" . $games[$i] . "</td>"; }
$i%5 === 0 <-- when you divide $i by 5 does it equal zero
Tank you, was i right it was a operator ? if it is do you maybe know a site where they explain it to me with that operator?
0

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>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.