你应该把括号内的变量和值case
后:
switch ($fixture->homeTeamName) {
case 'redBlackpool FC':
echo "équipe rouge";
break;
}
但是,也许你把太多的逻辑放到模板?也许你可以这样做:
switch ($fixture->homeTeamName) {
case 'redBlackpool FC':
$translatedHomeTeamName = "équipe rouge";
break;
}
,然后在模板:
<td><?php echo $translatedHomeTeamName; ?></td>
如果您不能修改<td>
的内容,那么你可以做:
switch ($fixture->homeTeamName) {
case 'redBlackpool FC':
$fixture->homeTeamName = "équipe rouge";
break;
}
上面应该在打印<td>
之前执行......基本上我只是建议你不要把整个switch语句放在<td>
之内,这样你就可以保持哟我们的代码整洁......
编辑 - 基于编辑的问题
既然你是一个foreach
循环中,那么你需要术语翻译的foreach里面,像这样:
<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) {
switch ($fixture->homeTeamName) {
case 'redBlackpool FC':
$fixture->homeTeamName = "équipe rouge";
break;
case 'blueBlackpool FC':
$fixture->homeTeamName = "équipe bleu";
break;
default:
// here you can do something in case the team isn't any of the above.
}
?>
<tr>
<td><?php echo $fixture->homeTeamName; ?></td>
<td>-</td>
<td><?php echo $fixture->awayTeamName; ?></td>
<td><?php echo $fixture->result->goalsHomeTeam; ?></td>
<td>:</td>
<td><?php echo $fixture->result->goalsAwayTeam; ?></td>
</tr>
<?php } ?>
更新 - 例如与功能 功能(你可以把它放在一个单独的文件,然后将其包含在主脚本):
function translateTeam($homeTeamName)
switch ($homeTeamName) {
case 'redBlackpool FC':
$homeTeamName = "équipe rouge";
break;
case 'blueBlackpool FC':
$homeTeamName = "équipe bleu";
break;
default:
// here you can do something in case the team isn't any of the above.
}
return $homeTeamName;
}
的foreach
循环:
// Include the php script that contains translateTeam() here...
<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { ?>
<tr>
// In the line below we call the function and pass the English name to it...
<td><?php echo translateTeam($fixture->homeTeamName); ?></td>
<td>-</td>
<td><?php echo $fixture->awayTeamName; ?></td>
<td><?php echo $fixture->result->goalsHomeTeam; ?></td>
<td>:</td>
<td><?php echo $fixture->result->goalsAwayTeam; ?></td>
</tr>
<?php } ?>
你写你从 “API” 输出。什么API?另外,你是否从更大的代码片段中获得第二个片段?如果是这样,你可以分享一大段代码吗? – Julian
带有json_decode的外部API,第二个片段只是一个测试! – Isabella
片段1和片段2在相同的PHP文件中?如果是这样,你能显示整个PHP文件吗?否则,你可以显示*两个* PHP文件(至少相关的部分)? – Julian