【问题标题】:Foreach inside If isset not working PHPForeach 里面如果 isset 不工作 PHP
【发布时间】:2016-05-24 11:06:26
【问题描述】:

我有一个选择 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)

【问题讨论】:

  • else 语句始终与if 一起使用,而不是与foreach 一起使用,在这里您将elseforeach 一起使用
  • 正确的方法是什么?如果职位等于会计,则该职位的位置列表将显示在选项值中,如果不是会计,则不会计的位置列表将仅使用 1 select html 标签显示在选项值中

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


【解决方案1】:

你的代码应该是这样的:

<?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>

【讨论】:

  • $hiring_location我会放选择查询来获取位置列表对吗?
  • 如果您的数据库中没有位置怎么办...如果您不想显示默认位置,那么只需删除 else 条件。我假设默认位置,例如孟买、浦那等不要在里面写查询。
【解决方案2】:

你的代码应该是这样的:

<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 应该根据您的要求,即您要显示的选择

【讨论】:

  • 所以我将在这个$yourLocation 变量中定义一个查询?
  • no.$yourLocation 是您要显示为选中的位置。例如New York(根据您的要求动态使用)
  • 是的,招聘位置来自数据库表,所以我需要先执行一个查询,然后才能在选项值中动态显示它。像这样 $query_hiring_location_non_mass = $wpdb->get_results('SELECT DISTINCThiring_location FROM resume_location WHEREhiring_location = "Utah" ORDER BYhiring_location ASC', OBJECT);
【解决方案3】:

@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>';
        }
    }

【讨论】:

  • 我怎样才能在else声明中得到$option-&gt;hiring_location
  • @Ranjith 从循环中删除花括号并尝试让我们看看您得到什么响应。例如:foreach($query_hiring_location_non_mass as $option) echo '&lt;option name="locations_list" class="filter_by" selected value="'. $option-&gt;hiring_location .'"&gt;'. $option-&gt;hiring_location .'&lt;/option&gt;'; } else { echo '&lt;option name="locations_list" class="filter_by" value="'. $option-&gt;hiring_location.'"&gt;'. $option-&gt;hiring_location .'&lt;/option&gt;'; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多