如果调用了某个变量,我需要一点帮助来让规则输出其他内容。为了将其分解,我列出了以下内容:
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链接,但在两个这些需要不同。要实现这一点,我需要输入什么?提前感谢(对不起,这一点难倒我了)。:)
发布于 2017-07-18 21:47:46
为此,您可以使用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的更多信息
发布于 2017-07-18 22:08:18
如果我没理解错的话,你想要的是对某些值有不同的输出。
我会考虑用另一个数组来保存你想要的不同输出的值,你可以这样做:
$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;
}
希望这能有所帮助。
https://stackoverflow.com/questions/45168477
复制相似问题