2013-07-14 80 views
1

我使用以下內容來顯示足球比賽的表格。它的工作原理,但有什麼辦法可以用循環或其他方法簡化它嗎?調用函數 - 我該如何簡化?

$con=mysqli_connect("SERVER","USER","PASS", "DATABASE") or die("I'm drunk officer."); 

//If blank, make it red. If it has a /, make it green, otherwise normal. 
function gw($week) 
{ 
if ($week == "") 
{ 
    echo "<td align='center' style='background: #FF0000'>"; 
} 
elseif (strpos($week,'/') !== false) 
{ 
    echo "<td align='center' style='background: #00FF00'>"; 
} 
else 
{ 
    echo "<td align='center'>"; 
} 
echo $week."</td>"; 
} 


function GetTeamFixtures($team) 
     { 
      global $con; 
      //Pull info from database 
      $queryget = mysqli_query($con, "SELECT * FROM `fixtures` WHERE team='$team' LIMIT 1"); 

      //go through every row 
      $row = mysqli_fetch_assoc($queryget); 

      //Get the info from the db 
      $team = $row['team']; 
      $gw1 = $row['gw1']; 
      $gw2 = $row['gw2']; 
      $gw3 = $row['gw3']; 
      $gw4 = $row['gw4']; 
      $gw5 = $row['gw5']; 
      $gw6 = $row['gw6']; 
      $gw7 = $row['gw7']; 
      $gw8 = $row['gw8']; 
      $gw9 = $row['gw9']; 
      $gw10 = $row['gw10']; 
      $gw11 = $row['gw11']; 
      $gw12 = $row['gw12']; 
      $gw13 = $row['gw13']; 
      $gw14 = $row['gw14']; 
      $gw15 = $row['gw15']; 
      $gw16 = $row['gw16']; 
      $gw17 = $row['gw17']; 
      $gw18 = $row['gw18']; 
      $gw19 = $row['gw19']; 
      $gw20 = $row['gw20']; 
      $gw21 = $row['gw21']; 
      $gw22 = $row['gw22']; 
      $gw23 = $row['gw23']; 
      $gw24 = $row['gw24']; 
      $gw25 = $row['gw25']; 
      $gw26 = $row['gw26']; 
      $gw27 = $row['gw27']; 
      $gw28 = $row['gw28']; 
      $gw29 = $row['gw29']; 
      $gw30 = $row['gw30']; 
      $gw31 = $row['gw31']; 
      $gw32 = $row['gw32']; 
      $gw33 = $row['gw33']; 
      $gw34 = $row['gw34']; 
      $gw35 = $row['gw35']; 
      $gw36 = $row['gw36']; 
      $gw37 = $row['gw37']; 
      $gw38 = $row['gw38']; 

      echo "<tr>"; 
      echo "<td>".$team."</td>"; 

      //Repeat for how many weeks needed 
      gw($gw15); 
      gw($gw16); 
      gw($gw17); 
      gw($gw18); 
      gw($gw19); 
      gw($gw20); 
      gw($gw21); 
      gw($gw22); 
      gw($gw23); 
      gw($gw24); 
      gw($gw25); 
      gw($gw26); 
      gw($gw27); 
      gw($gw28); 
      gw($gw29); 
      gw($gw30); 
      gw($gw31); 
      gw($gw32); 
      gw($gw33); 
      gw($gw34); 
      echo "</tr>"; 
     } 

      //Print out for each team 
     $team = "Chicago Fire"; GetTeamFixtures($team); 
     $team = "Chivas USA"; GetTeamFixtures($team); 
     $team = "Colorado Rapids"; GetTeamFixtures($team); 
     $team = "Columbus Crew"; GetTeamFixtures($team); 
     $team = "D.C. United"; GetTeamFixtures($team); 
     $team = "FC Dallas"; GetTeamFixtures($team); 
     $team = "Houston Dynamo"; GetTeamFixtures($team); 
     $team = "Los Angeles Galaxy"; GetTeamFixtures($team); 
     $team = "Montreal Impact"; GetTeamFixtures($team); 
     $team = "New England Revolution"; GetTeamFixtures($team); 
     $team = "New York Red Bulls"; GetTeamFixtures($team); 
     $team = "Philadelphia Union"; GetTeamFixtures($team); 
     $team = "Portland Timbers"; GetTeamFixtures($team); 
     $team = "Real Salt Lake"; GetTeamFixtures($team); 
     $team = "San Jose Earthquakes"; GetTeamFixtures($team); 
     $team = "Seattle Sounders FC"; GetTeamFixtures($team); 
     $team = "Sporting Kansas City"; GetTeamFixtures($team); 
     $team = "Toronto FC"; GetTeamFixtures($team); 
     $team = "Vancouver Whitecaps"; GetTeamFixtures($team); 
+1

爲什麼不直接使用數組? – str

回答

2

${'gw' . $i} = $row['gw'.$i];gw(${'gw' . $i});$i作爲抗衡。

請參閱http://php.net/manual/en/language.variables.variable.php

+1

然後調用函數'GetTeamFixtures'可以使用[array_walk](http://www.php.net/manual/en/function.array-walk.php)或[iterator-apply](http:// www。 php.net/manual/en/function.iterator-apply.php)使用一系列的團隊。 – alditis

+0

謝謝..這是完美的,並簡化了很多。我沒有意識到你可以做變量變量。 – Cully