2014-02-08 73 views
0

我是PHP新手。我有一些問題能夠使用嵌套for循環打印多個表。所以對於當前的代碼,我有一個嵌套for循環,只打印出1個表格,這與我在外部循環時得到的相同。我在數組中有一些硬編碼的數據,我希望嵌套for循環能夠打印出4張相同的表格。出於某種原因,這不是基於我的工作,我似乎無法弄清楚爲什麼它不是。在PHP中使用嵌套循環打印多個表格

最終,我們將從數據庫中提取硬編碼數據。我們必須從一個需要根據會議正確填充順序的數據庫中提取數據,並基於我目前的邏輯,我想知道這是否仍然是基於我指導自己的方式的可能性。我真的想弄明白這一點,並希望解釋爲什麼它不起作用。謝謝大家!

PHP代碼:

<?php print('<?xml version = "1.0" encoding = "utf-8"?>') ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <title>User selection page</title> 
    </head> 


    <?php 
     /* 
     Version 1.1: Instead of printing the teams in different cells we are going to print the 
     games in one row and we select the game itself to see if an upset will occur. 
     */ 

     require_once('Conference.php'); 

     for ($i = 0; $i<4; $i++) 
     {  
      $loadGameClass = new Conference(); 
      $loadGameClass->loadTeams(array("(1)Gonzaga vs (16)Southern U", "(8)Pittsburgh vs (9)Wichita St", "(5)Wisconsin vs (12)Ole Miss", "(4)Kansas st vs (13)Boise St", "(6)Arizona vs (11)Belmont", "(3)New Mexico vs (14) Harvard", "(7)Notre Dame vs (10)Iowa St", "(2)Ohio St vs (15) Iona")); 
      $teams = $loadGameClass->getTeams(); 

      echo '<table border="1">'; 

      for ($i = 0; $i < 8; $i++) 
      { 
       $highSeed = $teams[$i]; 
       echo '<tr><td>'.$highSeed.'</td><tr>'; 
      } 

      echo '</table>'; 
      echo "\n"; 
     } 
    ?> 

    <body> 
    </body> 
</html> 

回答

2

您在內環和外環採用$i。只需替換內部循環迭代器,例如通過$j和您的代碼正在工作。

+0

謝謝!這工作,我可能掃描了代碼100次,但我只是沒有注意到這個小小bug – user2522055

0

此代碼應工作的優良您:

<?php print('<?xml version = "1.0" encoding = "utf-8"?>') ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <title>User selection page</title> 
    </head> 
<body> 
<?php 
//added the php in the body tag because you can't add content between </head> and <body> 
      /* 
     Version 1.1: Instead of printing the teams in different cells we are going to print the 
     games in one row and we select the game itself to see if an upset will occur. 
     */ 

     require_once('Conference.php'); 

     for ($i = 0; $i<4; $i++) 
     {  
      $loadGameClass = new Conference(); 
      $loadGameClass->loadTeams(array("(1)Gonzaga vs (16)Southern U", "(8)Pittsburgh vs (9)Wichita St", "(5)Wisconsin vs (12)Ole Miss", "(4)Kansas st vs (13)Boise St", "(6)Arizona vs (11)Belmont", "(3)New Mexico vs (14) Harvard", "(7)Notre Dame vs (10)Iowa St", "(2)Ohio St vs (15) Iona")); 
      $teams = $loadGameClass->getTeams(); 

      echo '<table border="1">'; 

      for ($x = 0; $x < 8; $x++) //replaced $i by $x 
      { 
       $highSeed = $teams[$x];//replaced $i by $x 
       echo '<tr><td>'.$highSeed.'</td><tr>'; 
      } 

      echo '</table>'; 
      echo "\n"; 
     } 
    ?> 
</body> 
</html> 

你的代碼是行不通的,因爲你已經用了外環和內環相同的變量名。外循環的$ i每次都被第二個循環覆蓋。所以你沒有得到正確的桌子。

如果我理解正確,您希望從數據庫中打印表格中的結果並按大多數會議項目排序?您對替換$ loadGameClass至$球隊和這個替換:

$loadGame = mysql_query('SELECT * FROM teams ORDER BY most_conferenced ASC'); 

,你是內環應該是:

while($row = mysql_fetch_array($loadGame)){ 
    print '<tr><td>'.$row['match_text'].'</td></tr>'; 
} 

而且你的數據庫將是這樣的:

Teams: 
id:int primary key ai 
match_text:string 
conferenced:int 

這是你在找什麼?

+0

謝謝你的回答!我不太確定你的數據庫邏輯是如何工作的。這裏是一個鏈接http://espn.go.com/mens-college-basketball/tournament/bracket 正如你可以看到有4個會議。最外面的遊戲就是我現在正在使用的遊戲。我想在每場8場比賽中打印每場會議。我們必須正確地提取數據,並按照從上到下的順序排列每個會議的表格在支架中的外觀。我只想看看這在邏輯上是否可能與PHP,因爲我從來沒有做過任何與MySQL和PHP之前。 – user2522055