具有多选择下拉和引导的Isset [英] Isset with multi select dropdown and bootstrap
问题描述
我有一个WordPress搜索表单,它根据表单字段选择搜索帖子,用于自定义字段等。它工作正常,但是在搜索结果页面上我有一个除了我以外的表单的确切副本尝试根据搜索查询/ url字符串预设表单选择。
我使用常规选择下拉菜单,并将其设置为multiple,以便它可以使用bootstraps多选与复选框。我已经在这里提出了类似的问题,但是这是为了复选框,即使引导程序多选使用复选框,我仍然必须先使用select下拉菜单。
所以在尝试了几件事情之后,我接近了,但遇到了几个问题。在下面的代码中,我做了笔记来进一步解释我的意思。
< select name =property_type []id =pt-multiclass =form-control multi-select2multiple =multiple>
<?php
$ terms = get_terms(property-type,array('hide_empty'=> 0));
$ count = count($ terms);
$ b $ if($ count> 0){
echo< option value ='Any'> All< / option>;
foreach($ terms as $ term){
if(isset($ _ GET ['property_type'])){
foreach($ _GET ['property_type '] as $ proptypes){
//第一个例子
$ selected。=($ proptypes === $ term-> slug)? selected:; //显示第一个正确的选定值,但也选择它后面的所有内容,直到第二个正确的值,它不选择。
// $ selected =($ proptypes === $ term-> slug)? selected:; //只显示最后正确的选定值
// if($ proptypes === $ term-> slug){$ selected ='selected'; } //显示第一个正确的选定值,然后选择每个值,即使它没有被选中
//第二个示例
// $ selected。=($ proptypes === $ term-slug)? selected:; //显示第一个正确的选定值,然后选择每个值,即使它没有被选中
// $ selected =($ proptypes === $ term-> slug)? selected:; //只显示最后正确的选定值
// if($ proptypes === $ term-> slug){$ selected ='selected'; } //显示第一个正确的选定值,然后选择每个值,即使它没有被选中
}
}
echo< option value = '。 $ term-> slug。 '。 $选中。 > 中。 $ term->名称。 < /选项> 中; //第一个例子
// echo< option value ='。 $ term-> slug。 '。 ($ selected?'selected':'')。 > 中。 $ term->名称。 < /选项> 中; // SECOND EXMAPLE
}
}
?>
< / select>
创建数组并使用in_array()进行检查。
()); $ count = count(property-type,array('hide_empty'=> 0)); $ count = count ($ terms); //设置一个$ 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为$ term){$ selected =(in_array($ term-> slug,$ proptypes))? 'selected':'';回声<选项值='。 $ term-> slug。 '。 $选中。 > 中。 $ term->名称。 < /选项> 中;
I have a wordpress site with a search form that searches for posts based on the form field selections, for custom fields etc. It works fine, however on the search results page I have an exact copy of the form except that I am trying to preset the form selections based on the search query/url string.
I am using a regular select dropdown and I have set it to "multiple" so that it can use bootstraps multiselect with the checkboxes. I've asked a similar question HERE but that was for checkboxes and even though the bootstrap multiselect uses checkboxes, I still have to work with the select dropdown first.
So after trying several things, I've come close but have ran into several problems. In the code below I made notes to further explain exactly 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>
Create and array 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>
这篇关于具有多选择下拉和引导的Isset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!