仅当满足php变量条件时,才能显示我的选择选项值?

时间:2020-07-30 08:42:35

标签: php html

我有5个变量

$first ="first";
$second = "second";
$third = "third";
$fourth = "fourth";
$fifth = "fifth";

我在html中有这个选择选项

    <select name="e1" id="year" class="form-control" required="required">
      <option value="" selected="selected" style="background: #0356e8;color: white;">-  -</option>
      <option value="Attend To" style="background: #fd0303;color: white;">Attend To</option>
      <option value="Explore" style="background: #fd7803; color: white;">Explore</option> 
      <option value="Prompt" style="background: #fdd303; color: white;">Prompt</option>
      <option value="Unaided" style="background: #58d95e; color: white;">Unaided</option>
      <option value="Mastered" style="background: #12c4f9; color: white;">Mastered</option> 
      <option value="Not Applicable" style="background: grey; color: black;">Not Applicable</option>
    </select>

我该如何以一种方式使它$first ="first"不显示<option value="Attend To" style="background: #fd0303;color: white;">Attend To</option>……不应该显示。

如果$second = "second";,则不应显示第一个和第二个选项...

对于第三和第四也是如此。

1 个答案:

答案 0 :(得分:2)

要回答您的问题:

$variable='first / second ... '; // one variable

如果您不想在同一页面中使用IF,则可以在另一页面中使用function:

function HowMuch($variable){
if($variable=='first'){
echo '      
      <option value="Explore" style="background: #fd7803; color: white;">Explore</option> 
      <option value="Prompt" style="background: #fdd303; color: white;">Prompt</option>
      <option value="Unaided" style="background: #58d95e; color: white;">Unaided</option>
      <option value="Mastered" style="background: #12c4f9; color: white;">Mastered</option> 
      <option value="Not Applicable" style="background: grey; color: black;">Not Applicable</option>';
}elseif($variable=='second '){
echo '  
      <option value="Prompt" style="background: #fdd303; color: white;">Prompt</option>
      <option value="Unaided" style="background: #58d95e; color: white;">Unaided</option>
      <option value="Mastered" style="background: #12c4f9; color: white;">Mastered</option> 
      <option value="Not Applicable" style="background: grey; color: black;">Not Applicable</option>';
 }
}

现在是放置选择的页面:

<select name="e1" id="year" class="form-control" required="required">
  <option value="" selected="selected" style="background: #0356e8;color: white;">-  -</option>
      HowMuch($variable); //variable contains first or second..
    </select>