调用函数 - 如何简化此操作?

时间:2013-07-14 06:53:36

标签: php mysql function loops

我使用以下内容显示足球场的表格。它有效,但有没有办法用循环或其他东西来简化它?

$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 个答案:

答案 0 :(得分:2)

${'gw' . $i} = $row['gw'.$i];gw(${'gw' . $i}); $i是一个反击。

请参阅http://php.net/manual/en/language.variables.variable.php