2016-11-09 32 views
1

我對PHP很陌生。 我想在5行5列的5x5表中獲得遊戲(名稱)。 但我想通過遊戲的數字輸出生成表格,而不是預設的html表格。 我讓它工作,它顯示遊戲,但不是在表中。 你能幫我,給我一個很好的解釋,所以我也明白。 已經感謝!PHP如何在5x5表中排序數組的輸出?

<!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> 
+0

ü需要使用表外循環,但仍這將只打印1列,多行,而你的長度應該是'$ i <25',因爲你是從0開始的 – devpro

+0

達到目標的好方法。 @devpro評論顯示你需要修復的一些錯誤,然後你只需在每5場比賽之後添加開始和結束。你可以用$ i來檢查它的模除法是否給你提醒,這意味着它開始新行的時間。 –

+0

@Ranker對不起,我沒有得到它。你可以給我一個小費什麼的? –

回答

1

也許不是最乾淨的,但它的工作原理:)

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

我越來越6行,而不是5 –

+0

我的不好,開始計數在1而不是0,固定和更新 – Blinkydamo

+0

你能如果($ i%5 === 0)解釋我這是什麼,我不知道運算符$ i%5 === 0($ i = 0; $ i <25; $ i ++){ \t \t \t \t { \t \t \t \t \t echo''; \t \t \t \t} \t \t \t \t \t回聲 「​​」。 $遊戲[$ i]。 「」; \t \t \t \t} –

2

在代碼中,你必須要使用<table>外循環,比你必須需要定義5列比,因爲你需要,你可以用你的$games陣列:

例子:

<?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

+0

也得愛一個演示;-)旁註:爲了得到乾淨的HTML,最好是在每個元素後面添加'「\ n」'。這樣,HTML源代碼不會在單行中顯示爲一個大塊,這使得在需要查看HTML源代碼時很難調試。 –

+0

@ Fred-ii-:謝謝你的國王,你的意見對OP也有幫助.. – devpro

0

試試這個:

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