2016-08-01 106 views
2

我從API輸出:PHP開關問題

<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { ?> 
       <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 } ?> 

這工作得很好,它輸出的名字,如:華捷FC在:

<td><?php echo $fixture->homeTeamName; ?></td> 

現在我所要的輸出被翻譯成FR語言這樣的「隊報胭脂」,而不是「華捷FC」

我想在PHP頁面的標題這個PHP開關,但我得到黑屏:

switch ($teamName) { 
    case $fixture->homeTeamName['Walsall FC']: 
     echo "équipe rouge"; 
     break; 
} 

您的幫助表示讚賞。

+1

你寫你從 「API」 輸出。什麼API?另外,你是否從更大的代碼片段中獲得第二個片段?如果是這樣,你可以分享一大段代碼嗎? – Julian

+0

帶有json_decode的外部API,第二個片段只是一個測試! – Isabella

+1

片段1和片段2在相同的PHP文件中?如果是這樣,你能顯示整個PHP文件嗎?否則,你可以顯示*兩個* PHP文件(至少相關的部分)? – Julian

回答

5

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

輸出'​​<?php echo $ fixture-> homeTeamName; ?>' – Isabella

+0

請參閱編輯答案(最後一段) – mastazi

+0

首先回答工作正常,因爲獨立:'switch($ fixture-> homeTeamName){ case'redBlackpool FC': echo「équiperouge」; 休息; } $ fixture-> homeTeamName =「redBlackpool FC」; '但TD沒有在第二個答案的翻譯HomeHamTeamName中的變化 – Isabella