我使用下面的命令来显示一个足球装备表。它是有效的,但是有什么方法可以用循环或其他东西来简化它呢?
$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);
最佳答案
${'gw' . $i} = $row['gw'.$i];
和gw(${'gw' . $i});
其中$i
是计数器。
见http://php.net/manual/en/language.variables.variable.php
关于php - 调用函数-如何简化呢?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17637251/