2017-08-20 5 views
1

これを書く前に私はいくつかの研究をしました。 これは私がPHPでやった初めてのコードです。 私は<php include 'vars.php'; >を使用しましたが、これは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>"; 
?> 

ループを作成して、各ループで変数が変更され、プリセット名またはテキストが出力されます。私がここで期待していたことは、各ループの後に$xは "game1" "game2" "game3"などを出力し、私は既に変数をプリセットしているからです。

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

私は、内側<th>"GTA5""Dirt3""Skyrim"に変更することを考えました。

なぜこれが必要ですか?私はテーブルを作成しました。これはループしたい部分です。

<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> 
私は、この13回ループする必要

と各ループ、変更する必要がある"GTA5"名に。 最初の小さなコードは、より大きなものの "試し"に過ぎませんでした。

+0

私は '' $ games = array( "GTA5"、 "Dirt3"、Skyrim "); ... echo $ games [$ x-1];やそれに類するものです。より柔軟な[公式のドキュメントはこちら](https://secure.php.net/manual/en/language.types.array.php) –

答えて

2

$ game1、$ game2、$ game3などの代わりに、変数$ gameをゲームのリストが入った配列にします。

<?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>'; 
+0

私の答えがあなたの手助けをしてくれたら正解とマークしてください:) –

+0

ありがとうございました!foreach $ game AS $ gamename:Dも使用しました。コードは約270行(21400文字)から61行(2500文字)になりました。新しい「ゲーム」を追加するのはとても簡単です。 D –

+0

あなたの歓迎:)、あなたもまた配列として最小と最大FPSを追加することができますので、各配列は のようになります 1:GAME 1,4k、Min FPS = 20、最大FPS = 30 2:ゲーム1、1080p、最小FPS = 30、最大FPS = 45 –

関連する問題