2017-07-18 74 views
2

如果某個變量被調用,我需要一些幫助來獲得輸入其他內容的規則。 要打破它,我有以下幾種:

private $zebra_moto_symbol = array 
("ES400", "MC9500", "MC9200", "MC9190", "MC9094", "MC9090", "MC9097", "MC9060",; 

,並使用此代碼它拉模型到列表頁:

public function manufacturer_models_list() { 
    $manu_name = $this->manufacturer_name; 
    $output = "<ul>"; 

    sort($this->$manu_name); 
    foreach($this->$manu_name as $model) { 
     $output .= "<li>" . "<a href=\"repair.php\">" . $model . "</a></li>"; 
    } 

    $output .= "</ul>"; 
    $output .= "<p class=\"clear\"></p>"; 
    $output .= "<a href=\"repair.php\" " . "id=\"arrange-repair\">Arrange A Repair</a>"; 
    return $output; 
} 

在所有,但其中的兩個,我需要它顯示repair.php鏈接,但是這兩個需要不同。我需要輸入什麼才能做到這一點? 在此先感謝(對不起,這一個難倒我)。 :)

+1

你可以格式化你的代碼嗎(如果可能的話)在它目前的形式是有點難以閱讀。 – Neal

+1

歡迎來到[so]。花點時間探索問題編輯器的網站和功能。有一個不錯的''''按鈕,可用於將文本塊標記爲代碼。另外,請勿將所有代碼放在一行中,因爲它很難閱讀。在編輯器下,您可以預覽您的問題以瞭解它的外觀,並在提交前進行調整。 – axiac

回答

0

您可以使用switch聲明。

<? 
    public function manufacturer_models_list() { 
     $manu_name = $this->manufacturer_name; 
     $output = "<ul>"; 
     sort($this->$manu_name); 
     foreach ($this->$manu_name as $model) { 
      switch($model) { 
       //Output NOT repair.php on this list of strings 
       case "ES400": 
       case "MC9500": 
        $output .= "<li>DIFFERENT OUTPUT</a></li>"; 
        break; 
       //default is the action that happens if none of the previous conditions are met 
       default: 
        $output .= "<li>" . "<a href=\"repair.php\">" . $model . "</a></li>"; 
        break; 
      } 
     } 
     $output .= "</ul>"; 
     $output .= "<p class=\"clear\"></p>"; 
     $output .= "<a href=\"repair.php\" " . "id=\"arrange-repair\">Arrange A Repair</a>"; 
     return $output; 
    } 
?> 

瞭解更多關於Switch Statements

+0

這工作完美,非常感謝你:) –

0

如果我理解正確的,你想要的是有特定的值不同的輸出。

我曾經想有另一個數組來保存你想要一個不同的輸出值,你可以做這樣的事情:

$different_output_array = ['ES400', 'MC9500']; # you can add new elements any time 

,只是修改你的函數是這樣的:

public function manufacturer_models_list() { 
    $manu_name = $this->manufacturer_name; 
    $output = "<ul>"; 

    sort($this->$manu_name); 
    foreach($this->$manu_name as $model) { 

     if(in_array($model,$different_output_array)) 
     { 
      $output .= "<li>" . "<a href=\"another.php\">" . $model . "</a></li>"; 
     } 
     else 
     { 
      $output .= "<li>" . "<a href=\"repair.php\">" . $model . "</a></li>"; 
     } 


    } 

    $output .= "</ul>"; 
    $output .= "<p class=\"clear\"></p>"; 
    $output .= "<a href=\"repair.php\" " . "id=\"arrange-repair\">Arrange A Repair</a>"; 
    return $output; 
} 

希望這可以幫助。