你應該把括號內的變量和值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