2016-05-24 10 views
1

Ich habe eine Auswahl HTML-Tag. Ich muss die Option Werte anzeigen abhängig von der Jobbezeichnung im Eingabetext. Z.B. Wenn die Jobbezeichnung Buchhaltung ist, wird die Liste der Standorte für die Buchhaltung in den Optionswerten angezeigt.Foreach Inside Wenn Isset nicht funktioniert PHP

Aber mein Code funktioniert nicht. Bitte helfen Sie mir, dies zu beheben :(

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

Fehler:

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

'else' Aussage immer mit' verwenden if' nicht mit 'foreach' und hier verwenden Sie' else' mit 'foreach' –

+0

Was ist der richtige Weg? Wenn der Jobtitel dem Accounting entspricht, wird die Standortliste für diesen Jobtitel in den Optionswerten angezeigt, und wenn nicht Accounting, wird die Standortliste für Nicht Accounting in Optionswerte angezeigt. 1 Wählen Sie html-Tag nur – User014019

Antwort

2

Ihr Code wie folgt sein sollte:

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

in $ stelling_location "Ich werde die Select-Abfrage setzen, um die Standortliste richtig zu bekommen? – User014019

+0

Was ist, wenn Sie keine Speicherorte in Ihrer Datenbank haben ... Wenn Sie den Standardspeicherort nicht anzeigen möchten, dann löschen Sie einfach die Else-Bedingung. Ich habe diesen Standardort z.B. Mumbai, Pune usw. Schreibe die Abfrage nicht darin. – sAcH

1

Ihr Code sollte wie folgt aus:

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

Hier sollte $yourLocation wie sein pro Ihre Anforderung das heißt, die Sie

+0

also definiere ich eine Abfrage in dieser '$ yourLocation' Variable? – User014019

+0

Nein. "$ YourLocation" ist der Ort, den Sie anzeigen möchten. zum Beispiel 'New York' (verwenden Sie dynamisch gemäß Ihrer Anforderung) –

+0

Ja, der Einstellungsstandort stammt aus der Datenbanktabelle, daher muss ich eine Abfrage durchführen, bevor ich sie dynamisch in den Optionswerten anzeigen kann. so $ query_hiring_location_non_mass = $ wpdb-> get_results ('SELECT DISTINCT stelling_location VON resume_location WHERE stellungsort = "Utah" ORDER BY hosting_location ASC', OBJECT); – User014019

0

@user ausgewählt zeigen wollen, ich glaube, Sie zwei Blumenbügel verpassten die Schleife verwenden diese stattdessen zu schließen:

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>'; 
     } 
    } 
+0

wie bekomme ich '$ option-> housing_location' in' else' Anweisung –

+0

@Ranjith entfernen Sie die Blume Klammern aus der Schleife und versuchen Sie können sehen, welche Antwort Sie bekommen. zB: 'foreach ($ query_hiring_location_non_mass als $ option) echo ' '; } sonst { echo ' '; } ' – shan2batman

+0

@Ranjith hat es funktioniert – shan2batman