1

Before writing this I did some research. This is my first-ever code I did in PHP. I did use <php include 'vars.php'; > but that was just a copy paste from google.

<?php
echo "<table>";
for ($x = 1; $x <= 10; $x++) {
    ${'game' . $x} = $x;
    $game1 = "GTA5";
    $game2 = "Dirt3";
    $game3 = "Skyrim";
    echo "<th>".$game."</th>";
}
echo "</table>";
?>

I want to make a loop, where the variable changes on each loop and outputs a preset name or text. What I was hoping will happen here is, that after each loop the $x will output "game1" "game2" "game3" etc, and because I've already preset the variables.

$game1 = "GTA5";
$game2 = "Dirt3";
$game3 = "Skyrim";

I thought that the <th> inner will change to "GTA5" "Dirt3" "Skyrim".

Why do I need this? I have created a Table and this is a part that I want to loop.

<th><div class='text'>GTA5</div><div class='grid'><div onclick='()' class="res">1080p ></div><div onclick='()' class="res">1440p ></div><div onclick='()' class="res">4K ></div></div></th>
<th><div class="text"><p>GTA5</p><span>1080p</span></div><div onclick='()' class="btn">></div></th>
<th><div class="text"><p>GTA5</p><span>min FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>max FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>used Vram</span></div></th>
<th><div class="text"><p>GTA5</p><span>1440p</span></div><div onclick='()' class="btn">></div></th>
<th><div class="text"><p>GTA5</p><span>min FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>max FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>used Vram</span></div></th>
<th><div class="text"><p>GTA5</p><span>4k</span></div><div onclick='()' class="btn">></div></th>
<th><div class="text"><p>GTA5</p><span>min FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>max FPS</span></div></th>
<th><div class="text"><p>GTA5</p><span>used Vram</span></div></th>

I need to loop this 13 times and on each loop, the name "GTA5" needs to change. The first tiny code was just a "try" for the bigger thing.

1
  • I'd do $games = array("GTA5", "Dirt3, "Skyrim"); ... echo $games[$x-1]; or the like. Much simpler, much more flexible. Official documentation here. Commented Aug 20, 2017 at 23:33

1 Answer 1

2

Instead of having $game1, $game2, $game3 etc, turn the variable $game into an array with the list of games inside.

<?php

$game = array("GAME1","GAME2","GAME3","ETC");
$screen = array("4K","1080p","720p","480p");

echo '<table>';
$i=0;

while($i <4){

echo '<th><div class="text"><p>'.$game[$i].'</p><span>'.$screen[$i].'</span></div><div onclick="()" class="btn"></div></th>';
echo '<th><div class="text"><p>'.$game[$i].'</p><span>min FPS</span></div></th>';
echo '<th><div class="text"><p>'.$game[$i].'</p><span>max FPS</span></div></th>';
$i++;
}
echo '<table>';
Sign up to request clarification or add additional context in comments.

8 Comments

If my answer helped you with what you wanted please mark it at as the correct answer :)
Yeah, thank you so much! I used also foreach $game AS $gamename :D The code went from about 270lines (21400 chars) to 61lines (2500 chars) and it's so easy to add new "games" to it :D Just adding 1 Value and bam.. rest does itself :D
Your welcome :), you can also add min and max FPS as arrays too, so then each array will be like 1: GAME 1, 4k , Min FPS = 20, Max FPS = 30 2: GAME 1, 1080p , Min FPS = 30, Max FPS = 45
I did all this already :) ue4faq.com This is just a test. I want now to add ID's to elements to make a show hide, but not really any idea how to do it.
you can use array_search function like this: array_search("GAME1",$game) which will give you a return of value or use it like this: array_search("GAME1",$game,true); which will give you the return of the key
|

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.