Foreach inside如果isset不工作PHP

时间:2016-05-24 11:06:26

标签: php if-statement select drop-down-menu foreach

我有一个选择的html标签。我需要显示选项值取决于输入文本中的作业标题。例如。如果职务名称是会计科目,则会计科目的位置列表将显示在选项值中。

但是我的代码无效。请帮我解决这个问题:(

<select name="locations_list" id="filterbyhiringlocation">
    <option name="default" class="filter_by" selected="selected" value="Select by Location">Select by Location</option>
    <?php
    if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'Sales Associate'){
        foreach($query_hiring_location_mass as $option){
            echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
        } else {
            echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
        }
    } else if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'District Manager'){
        foreach($query_hiring_location_non_mass as $option){
            echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
        } else {
            echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
        }
    }
        ?>
</select>

错误:

Parse error: syntax error, unexpected 'else' (T_ELSE)

3 个答案:

答案 0 :(得分:2)

您的代码应该是这样的:

<?php
$hiring_location = 'YOUR DEFAULT LOCATION';
?>
<select name="locations_list" id="filterbyhiringlocation">
    <option name="default" class="filter_by" selected="selected" value="Select by Location">Select by Location</option>
    <?php
    if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'Sales Associate'){
        if(count($query_hiring_location_mass)) {
            foreach($query_hiring_location_mass as $option){
                echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
            }
        }
        else {
            echo '<option name="locations_list" class="filter_by" value="'. $hiring_location.'">'. $hiring_location .'</option>';
        }
    } else if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'District Manager'){
        if(count($query_hiring_location_non_mass)) {
            foreach($query_hiring_location_non_mass as $option){
                echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
            }
        }
         else {
            echo '<option name="locations_list" class="filter_by" value="'. $hiring_location.'">'. $hiring_location .'</option>';
        }
    }
        ?>
</select>

答案 1 :(得分:1)

你的代码应该是这样的:

<select name="locations_list" id="filterbyhiringlocation">
    <option name="default" class="filter_by" selected="selected" value="Select by Location">Select by Location</option>
    <?php
    if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'Sales Associate'){
        foreach($query_hiring_location_mass as $option){
            if ($option->hiring_location == $yourLocation) {
                echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
            } else {
                echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
            }
        }
    } else if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'District Manager'){
        foreach($query_hiring_location_non_mass as $option){
            if ($option->hiring_location == $yourLocation) {
                echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
            } else {
                echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
            }
        }
    }
        ?>
</select>

此处$yourLocation应符合您的要求,即您要显示的选项

答案 2 :(得分:0)

@user我认为你错过了两个花括号来关闭循环使用它代替:

    if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'Sales Associate'){
        foreach($query_hiring_location_mass as $option){
            echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
} 
       } else {
            echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
        }
    } else if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'District Manager'){
        foreach($query_hiring_location_non_mass as $option){
            echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
        }
} else {
            echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
        }
    }