将ISSET与复选框一起使用 [英] Using ISSET with checkboxes

查看:64
本文介绍了将ISSET与复选框一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wordpress搜索表单来优化当前搜索,而林正试图做的事情是在搜索结果页面中包含来自的搜索,并根据查询设置其值。

I am working on a wordpress search form to refine the current search and what Im trying to do is have the search results page with the search from and it's values set based on the query.

到目前为止,我已经成功完成了此操作,其中包含单个选择下拉列表和单个复选框,例如-

So far I've been successful in doing so with single select drop downs and single checkboxes like so --

<!-- SINGLE SELECT -->
    <select name="baths" class="form-control">
    <?php if (isset($_GET['baths'])) {
        $bths = $_GET['baths']; ?>
    <option value="<?php echo $bths; ?>"><?php echo $bths; ?></option>  
    <?php } else { ?>
    <option value="Any">Any</option>
    <?php } ?>
    <option value="Any">Any</option>
    <option value="1">1+</option>
    <option value="2">2+</option>
    <option value="3">3+</option>
    <option value="4">4+</option>
    <option value="5">5+</option>
    <option value="6">6+</option>
    <option value="7">7+</option>
    <option value="8">8+</option>
    <option value="9">9+</option>
    <option value="10">10+</option>
    </select>

<!-- SINGLE CHECKBOX -->
<input type="checkbox" name="dogs" class="styled" value ="yes" <?php if (isset($_GET['dogs'])) { ?>checked<?php } ?>>

这可行,但对于多个值却无效。这是我生成一组复选框以选择便利设施的功能-

That works, but for the multiple values it doesn't. This is my function to generate a set of checkboxes to select amenities -

<?php
$amenity_array = array();
$id            = get_query_var('site');
if (!empty($id)) {
  $property_amenities = get_post_meta($id, 'imic_property_amenities', true);
  global $imic_options;
  foreach ($property_amenities as $properties_amenities_temp) {
    if ($properties_amenities_temp != 'Not Selected') {
      array_push($amenity_array, $properties_amenities_temp);
    }
  }
}
global $imic_options;
if (isset($imic_options['properties_amenities']) && count($imic_options['properties_amenities']) > 1) {
  foreach ($imic_options['properties_amenities'] as $properties_amenities) {
    $am_name = strtolower(str_replace(' ', '', $properties_amenities));
    $check   = '';
    if (in_array($properties_amenities, $amenity_array)) {
      $check = 'checked="checked"';
    }

<!-- HERE I TRY TO FIND THE SELECTED CHECKBOXES AND CHECK THEM OFF -->
    if (isset($_GET['p_am'])) {
      $ams = $_GET['p_am'];

      echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
    } else {
      echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
    }
<!-- END ISSET -->

  }
} else {
  _e('There is no Properties Amenities', 'framework');
}
?>

对于多选下拉列表,我正在使用引导多选,因此在我的模板上,代码如下所示-

For the multi select drop down I am using bootstrap multiselect, so on my template the code looks like this --

<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 ) {
     echo "<option value='" . $term->slug . "'>" . $term->name . "</option>";
   }
 }
?>
</select>

在页面上显示为---

On the page it renders out as ---

<select name="property_type[]" id="pt-multi" class="form-control multi-select2 iOSselect" multiple="multiple" style="display: none;">
  <option value="Any">All</option>
  <option value="co-op">Co-Op</option>
  <option value="condo">Condo</option>
</select>

<div class="btn-group" style="width: 100%;">
  <button type="button" class="multiselect dropdown-toggle btn btn-default form-control multi-select2" data-toggle="dropdown" title="Property Type" style="width: 100%; overflow: hidden; text-overflow: ellipsis;">
  <span class="multiselect-selected-text">Property Type</span> 
  <b class="caret"></b></button>
  <ul class="multiselect-container dropdown-menu pull-right">
    <li class="multiselect-item multiselect-all">
      <a tabindex="0" class="multiselect-all">
        <label class="checkbox"><input type="checkbox" value="multiselect-all">  Select all</label>
      </a>
    </li>
    <li>
      <a tabindex="0"><label class="checkbox">
        <input type="checkbox" value="Any"> All</label>
      </a>
    </li>
    <li>
      <a tabindex="0"><label class="checkbox"><input type="checkbox" value="co-op"> Co-Op</label>
      </a>
    </li>
    <li>
      <a tabindex="0"><label class="checkbox"><input type="checkbox" value="condo"> Condo</label>
      </a>
    </li>
  </ul>
</div>

有什么想法吗?

更新

从我的便利设施上方使用我的代码复选框会在页面上呈现出来---

Using my code from above my amenities checkbox renders out on the page like so ---

<div>
  <label>amenities</label>
  <div class="checkbox">
    <input type="checkbox" name="p_am" class="styled" value="Doorman (Full Time)">
    <label for="doorman(fulltime)">Doorman (Full Time)</label>
  </div>

  <div class="checkbox">
    <input type="checkbox" name="p_am" class="styled" value="Doorman (Part Time)"><label for="doorman(parttime)">Doorman (Part Time)</label>
  </div>

  <div class="checkbox">
    <input type="checkbox" name="p_am" class="styled" value="Laundry (In-Unit)"><label for="laundry(in-unit)">Laundry (In-Unit)</label>
  </div>

  <div class="checkbox">
    <input type="checkbox" name="p_am" class="styled" value="Fitness Center"><label for="fitnesscenter">Fitness Center</label>
  </div>
</div>

此功能与我的搜索功能配合使用,允许用户检查要在查询字符串中包括的设施-

This works with my search function allowing users to check off which amenities to include in the query string --

p_am=Doorman+%28Full+Time%29&p_am=Indoor+Pool

我的搜索功能广泛,但到目前为止,与便利设施有关的部分-

My search function is extensive but as far the relevant parts to the amenities -

$amenities = isset($_GET['p_am'])?$_GET['p_am']:'';
$amenities = ($amenities == __('Any', 'framework')) ? '' : $amenities;

// .....

if (!empty($amenities)) {
  array_push($meta_query,array(
    'key' => 'imic_property_amenities',
    'value' => $amenities,
    'compare'=>'LIKE'
  ));
}

更新

关于设置引导多重选择的下拉选项,我尝试使用以下代码设置选项-

In regards to setting the dropdown options for the bootstrap multiselect, I'm trying to set the options using the following code -

<?php 

$taxonomyName = "city-type";

$parent_terms = get_terms( $taxonomyName, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );

echo "<select name='property_nhb[]' class='form-control multi-select' id='p-nhb' multiple>";

foreach ( $parent_terms as $pterm ) {
 // echo "<option data-val='" . $pterm->slug . "' value='Any' " . $selected . ">Any</option>";
    //Get the Child terms
    $terms = get_terms( $taxonomyName, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
    foreach ( $terms as $term ) { 

if(isset($_GET['property_nhb[]'])) {
$pnhb = $_GET['property_nhb'];

$selected = 'selected';

}

        echo "<option data-val='" . $pterm->slug . "' value='" . $term->slug . "' " . $selected . " >" . $term->name . "</option>"; 
    }
}
echo "</select>"; 
?>




我当前的搜索功能代码


推荐答案

创建搜索表单似乎有些倒退-似乎更像是为每个结果生成一个表单?以下带注释的代码和注释。我会尽量保持您的编码风格。

This seems a little backwards for creating a search form - it seems more like you're generating a form for each individual result? Annotated code and comments below. I've tried to keep to your coding style in my answer.

$amenity_array = array();
$id            = get_query_var('site');
// this first section gets the selected meta properties for the queried property (if available)
// [skipping for brevity ]
global $imic_options;
if (isset($imic_options['properties_amenities']) && count($imic_options['properties_amenities']) > 1) {
  // this loops over all of the globally defined properties
  foreach ($imic_options['properties_amenities'] as $properties_amenities) {
    // gets the name of the property
    $am_name = strtolower(str_replace(' ', '', $properties_amenities));
    // and sets up to check the box if the property has the defined amenity
    // if this is for a search form, why check boxes based on a result?
    $check   = '';
    if (in_array($properties_amenities, $amenity_array)) {
      // note: this only really needs to be 'checked'
      $check = 'checked="checked"';
    }

不过,这里的用途有点交叉。

Here's where things go a little cross-purposes, though.

    if (isset($_GET['p_am'])) {
      $ams = $_GET['p_am'];

      echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
    } else {
      echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
    }

这表示所有复选框都具有相同的名称( p_am ),而不是使用循环名称( $ am_name )或某种形式的组合(如果您希望在同一搜索中使用所有便利设施)输入数组(例如 p_am [$ am_name] )。

This says that all checkboxes have the same name ("p_am") instead of using the loop name for the amenity ($am_name) or some form combination if you want all amenities in the same search input array (e.g. "p_am[$am_name]").

如果提供给 $ _ GET,每个复选框也会更改 $ check 的值数组。

Each checkbox would also change the value of $check if supplied to the $_GET array.

      $ams = $_GET['p_am'];
      if (isset($ams[$am_name])) {
        $check = 'checked';
      }

每个复选框的 name = p_am [' 。$ am_name。'] 作为名称。

Each checkbox would have name="p_am['.$am_name.']" as the name.

      echo '<div class="checkbox"><input type="checkbox" name="p_am[' . $am_name . ']" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';

如果您希望每个便利设施都具有唯一的名称(请查看原始复选框,而不是被称为 p_am ),并且不在PHP数组中,那么您只需要使用循环名称( $ am_name ),像这样:

If you want each of the amenities to have unique names (looking at the original checkbox which isn't called p_am at all) and not be in an array in PHP, then you would just use the loop name for the amenity ($am_name), like so:

      if (isset($_GET[$am_name])) {
        $check = 'checked';
      }

每个复选框的 name ='。$ am_name。' 作为名称。

Each checkbox would have name="'.$am_name.'" as the name.

更新::OP更新后,每个复选框看起来像需要具有相同的名称,但不能是键值。对于这种情况,您的复选框应名为 p_am [] (在两个搜索页面上均为原始页面),并且您需要使用类似 in_array() 而不是 isset()检查结果,像这样:

UPDATE: after the OP was updated, it looks like every checkbox needs to have the same name, but not be keyed values. For this situation, your checkboxes should be called p_am[] (on both search page an original page), and you need to use something like in_array() instead of isset() to check the result, like so:

      if (in_array($properties_amenities, $_GET['p_am'])) {
        $check = 'checked';
      }

附加说明-您还使用了 $ am_name 的标签,而没有在复选框上实际设置 id 属性以匹配,并且不会去除非 id 字符(如括号),因此标签关联将不起作用。

Additional note - you're also using $am_name for the label, without actually setting the id attribute on the checkbox to match to, as well as not stripping out non-id characters (like parentheses), so the label association won't work.

      echo '<div class="checkbox"><input type="checkbox" name="' . $am_name . '" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';

对于bootstrap select,您只是错过了选择了哪些选项的检查:

For the bootstrap select, you're just missing a check for which options are selected:

<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 the option is 'checked', you need to add the 'selected' atttibute here
    echo "<option value='" . $term->slug . "'>" . $term->name . "</option>";
  }
}
?>
</select>

所以循环内部看起来像这样:

So the inside of the loop would look something like this instead:

    // for efficiency's sake should live outside the loop as a one-off, but here for illustrative purposes
    $types = $_GET['property_type'];
    $selected = isset($types[$term->slug]) ? 'selected' : '';
    echo "<option value='" . $term->slug . "'" . $selected . ">" . $term->name . "</option>";

如果您使用的是引导选择的旧版本,则可能需要将选定的字符串'selected = selected data-selected = true 取决于您使用的版本。对于当前版本,上面的字符串应该可以。

If you're using an older version of bootstrap select, you may need to have the selected string be 'selected="selected" or data-selected="true", depending on the version you're using. For a current version, the above string should be fine.

如果< select> 的含义不是成为选项数组(即不是多个选项),则应从名称中删除 [] ,其操作方式与复选框代码类似:

If the <select> isn't meant to be an array of options either (i.e. not multiple), then the [] should be removed from the name, and would operate similarly to the checkbox code:

<select name="property_type" id="pt-multi" class="form-control multi-select2">
<?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 ) {
    $type = $_GET['property_type'];
    $selected = isset($type) && $type == $term-slug ? 'selected' : '';
    echo "<option value='" . $term->slug . "'" . $selected . ">" . $term->name . "</option>";
  }
}
?>
</select>

< select> 实际上也不是工作的-它所做的就是在选项列表的顶部添加重复的< option> 被选中,表单会将在顶部创建的重复项视为< select> 的已选择。

The original HTML for the <select> doesn't actually 'work' as such either - all it's doing is adding a duplicate <option> to the top of the options list, and because nothing is selected, the form will treat the duplicate created at the top as 'selected' for the <select>.

更新2:的原因是搜索功能中断了一组复选框输入,是因为您使用的是 Like 元查询的比较。来自有关WordPress Meta Query的文档

UPDATE 2: the reason that the search function is breaking with an array of checkbox inputs is because you are using the LIKE comparison for your meta query. From the documentation on WordPress Meta Query:


仅当compare为'IN','NOT IN','BETWEEN'或'NOT BETWEEN'时,它才可以是数组。

It can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'.

文档还显示了如何将它们连接在一起的示例。由于您尚未提供搜索查询的实际方式,这有点猜测,但是对于一个复选框数组来说,您的代码应该类似于:

The documentation also shows examples of how to join these together. Since you haven't supplied how you're actually making the search query this is guessing a little, but it looks like for an array of checkboxes your code should be something similar to:

if (!empty($amenities)) {
  foreach ($amenities as $amenity) {
    array_push($meta_query, array(
      'key' => 'imic_property_amenities',
      'value' => $amenity,
      'compare' => 'LIKE'
    ));
  }
}

与文档中的示例一样,您将需要确保存在 relation => ‘OR’ relation => 和 定义您希望搜索的操作方式。

As with the examples in the documentation, you'll need to make sure there's a relation => 'OR' or relation => 'AND' to define how you want your search to operate.

这篇关于将ISSET与复选框一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆