Php 需要帮助从复制/粘贴的if语句中创建循环吗

Php 需要帮助从复制/粘贴的if语句中创建循环吗,php,html,if-statement,Php,Html,If Statement,我有一个从数据库中获取信息并将其显示在表中的页面。它确实有效,但非常麻烦 //Table header here echo "<tr><td>".$teamname."</td>"; //For Gameweek 7 if ($gw7 == "") { //The team has no game - highlight the cell in red echo "<td align='center' style='backgrou

我有一个从数据库中获取信息并将其显示在表中的页面。它确实有效,但非常麻烦

//Table header here
echo "<tr><td>".$teamname."</td>";

//For Gameweek 7
if ($gw7 == "") 
{ 
    //The team has no game - highlight the cell in red
    echo "<td align='center' style='background: #FF0000'>"; 
}
    elseif (strpos($gw7,'/') !== false) 
{ 
    //The team has 2 games this week - highlight it in green
    echo "<td align='center' style='background: #00FF00'>"; 
}
else
{
    //this means the team has a single game this week - normal cell.
    echo "<td align='center'>";
}
echo $gw7."</td>";
echo "</tr>";

//for gameweek 8 to 36, the above for loop is just repeated (mostly copy/pasted)

//Table footer here
//这里是表头
回显“$teamname.”;
//第7周比赛
如果($gw7==“”)
{ 
//球队没有比赛-用红色突出显示单元格
回声“;
}
elseif(strpos($gw7,“/”)!==false)
{ 
//球队本周有两场比赛-以绿色突出显示
回声“;
}
其他的
{
//这意味着球队本周只有一场比赛——普通牢房。
回声“;
}
echo$gw7。”;
回声“;
//对于第8至36周的游戏,上述for循环只是重复(主要是复制/粘贴)
//这里是表尾
有没有更干净的方法?我不喜欢多次复制/粘贴同一代码


游戏周被称为
$gw7
$gw8
$gw9
$gw10
等,以文本形式包含球队面对的对手。7美元gw7代表了本赛季的第7场比赛。它们被分成游戏周。如果不清楚,请告诉我。

创建这样的函数

function gw($week) {
    //For Gameweek 7
    if ($week == "") 
    { 
        //The team has no game - highlight the cell in red
        echo "<td align='center' style='background: #FF0000'>"; 
    }
    elseif (strpos($week,'/') !== false) 
    { 
        //The team has 2 games this week - highlight it in green
        echo "<td align='center' style='background: #00FF00'>"; 
    }
    else
    {
        //this means the team has a single game this week - normal cell.
        echo "<td align='center'>";
    }
    echo $week."</td>";
}

//Table header here
echo "<tr><td>".$teamname."</td>";
gw($gw7); // call here function for $gw7 for others as well
echo "</tr>";
功能gw($week){
//第7周比赛
如果($week==“”)
{ 
//球队没有比赛-用红色突出显示单元格
回声“;
}
elseif(strpos($week,“/”)!==false)
{ 
//球队本周有两场比赛-以绿色突出显示
回声“;
}
其他的
{
//这意味着球队本周只有一场比赛——普通牢房。
回声“;
}
回声$week.“;
}
//这里是表格标题
回显“$teamname.”;
千兆瓦(7千兆瓦);//对于其他函数,也可以在此处调用$gw7
回声“;

将gameweeks放在一个数组中,迭代并调用数组索引大于6的自定义代码

function table_cell($color) {
    echo "<td align='center' style='background: $color'>"; 
}

//Table header here
echo "<tr><td>".$teamname."</td>";

//For Gameweek 7
if ($gw7 == "") 
{ 
    table_cell('#FF0000');
    //The team has no game - highlight the cell in red

}
    elseif (strpos($gw7,'/') !== false) 
{ 
    table_cell('#00FF00');
    //The team has 2 games this week - highlight it in green
}
else
{
    //this means the team has a single game this week - normal cell.
    echo "<td align='center'>";
}
echo $gw7."</td>";
echo "</tr>";
函数表\单元($color){
回声“;
}
//这里是表格标题
回显“$teamname.”;
//第7周比赛
如果($gw7==“”)
{ 
表#单元('#FF0000');
//球队没有比赛-用红色突出显示单元格
}
elseif(strpos($gw7,“/”)!==false)
{ 
表#单元('#00FF00');
//球队本周有两场比赛-以绿色突出显示
}
其他的
{
//这意味着球队本周只有一场比赛——普通牢房。
回声“;
}
echo$gw7。”;
回声“;
$team_array=array(“team1”、“team2”、“team3”、“team4”)//你想要多少队
foreach($team_数组作为$teamname)
{
$rows=getfixturesfromdb($teamname);
//如果装置类似于$rows['gw1']、$rows['gw2']..等
foreach($gw)
{
回显“$teamname.”;
如果($gw==“”)
{ 
//球队没有比赛-用红色突出显示单元格
回声“;
}
elseif(strpos($gw,“/”)!==false)
{ 
//球队本周有两场比赛-以绿色突出显示
回声“;
}
其他的
{
//这意味着球队本周只有一场比赛——普通牢房。
回声“;
}
echo$gw。”;
回声“;
}
}

似乎很清楚,只是使用一个
$bgcolor
变量,并在条件后回显
”?我不确定这会简化很多。只有两种颜色。。绿色和红色。。。虽然把它放在$bgcolor意味着如果我想全部改变它,我只会改变一次。。。。好的计划。听起来更像是一个候选人。谢谢-这看起来是我想要的,但是
gw(#gw7)后面的#符号是什么它给了我一个语法错误。这就是将变量传递给函数的方式吗?@Cully很抱歉,这是打字错误,应该是
$
而不是
.
。愚蠢的错误…现在已编辑。谢谢。。我认为这是一种将变量传递给函数的方法,我以前从未见过这种方法,所以我开始过度思考它。它现在工作得很好,非常感谢!节省了我以后的大量编辑,因为我只需要编辑一个函数。
 $team_array=array("team1","team2","team3","team4"); //as many teams you want
 foreach($team_array as $teamname)
 {
    $rows=getfixturesfromdb($teamname);
    //if fixtures are like $rows['gw1'],$rows['gw2']....etc
    foreach($row as  $gw)
    {
       echo "<tr><td>".$teamname."</td>";
       if ($gw == "") 
     { 
          //The team has no game - highlight the cell in red
          echo "<td align='center' style='background: #FF0000'>"; 
     }
         elseif (strpos($gw,'/') !== false) 
     { 
        //The team has 2 games this week - highlight it in green
        echo "<td align='center' style='background: #00FF00'>"; 
      }
        else
      {
         //this means the team has a single game this week - normal cell.
         echo "<td align='center'>";
      }
         echo $gw."</td>";
         echo "</tr>";
     }
 }