Iset with a drop-down list with several selections and bootstrap

I have a wordpress site with a search form that searches for messages depending on the choice of the form field, for custom fields, etc. This works fine, however on the search results page I have an exact copy of the form, except that I'm trying to pre-select the form based on the search string / url.

I use the conventional selection drop-down menu, and I set it to β€œmultiple” so that it can use multiboot bootstraps with checkboxes. I asked a similar question HERE , but it was for checkboxes, and even though checkboxes are used in the multiselect bot, I still have to work with the dropdown list first.

So, after several attempts, I came close, but ran into several problems. In the code below, I took notes to explain again what I mean.

<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);

if ( $count > 0  ){
        echo "<option value='Any'>All</option>";

        foreach ( $terms as $term ) {

if (isset($_GET['property_type'])) {
        foreach ($_GET['property_type'] as $proptypes) {

// FIRST EXAMPLE
$selected .= ($proptypes === $term->slug) ? "selected" : "";  // shows first correct selected value but also selects everything after it up until the second correct value, which it doesn't select.
//$selected = ($proptypes === $term->slug) ? "selected" : "";   // shows only last correct selected value
//if ($proptypes === $term->slug) { $selected = 'selected'; }   // shows first correct selected value then selects every value after, even if it wasn't selected

// SECOND EXAMPLE
//$selected .= ($proptypes === $term->slug) ? "selected" : "";  // shows first correct selected value then selects every value after, even if it wasn't selected
//$selected = ($proptypes === $term->slug) ? "selected" : "";   // shows only last correct selected value
//if ($proptypes === $term->slug) { $selected = 'selected'; }   // shows first correct selected value then selects every value after, even if it wasn't selected


    } 
}
      echo "<option value='" . $term->slug . "' " . $selected . ">" . $term->name . "</option>";                  // FIRST EXAMPLE

      //echo "<option value='" . $term->slug . "' " . ($selected?' selected':'') . ">" . $term->name . "</option>"; // SECOND EXMAPLE 

    }
}
?>
</select>
0
source share
1 answer

Create and massage and use in_array () to check.

<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms("property-type", array('hide_empty' => 0));
$count = count($terms);

// Setup an array of $proptypes
$proptypes = array();
if (isset($_GET['property_type'])) {
    foreach ($_GET['property_type'] as $proptype) {
        $proptypes[] = $proptype;
    }
}

if ($count > 0) {
    echo "<option value='Any'>All</option>";
    foreach ($terms as $term) {
        $selected = (in_array($term->slug, $proptypes)) ? 'selected' : '';
        echo "<option value='" . $term->slug . "' " . $selected . ">" . $term->name . "</option>";
    }
}

?>
</select>
Run codeHide result
+1
source

Source: https://habr.com/ru/post/1690031/


All Articles