duanfengdian7014 2013-07-14 06:53
浏览 36
已采纳

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

I use the following to display a table of soccer fixtures. It works, but is there any way to simplify it with a loop or something?

$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条回答 默认 最新

  • duanjipiao7076 2013-07-14 06:57
    关注

    ${'gw' . $i} = $row['gw'.$i]; and gw(${'gw' . $i}); with $i being a counter.

    see http://php.net/manual/en/language.variables.variable.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法