将 ISSET 与复选框一起使用

Using ISSET with checkboxes

我正在使用 wordpress 搜索表单来优化当前搜索,而我想做的是让搜索结果页面包含来自的搜索以及基于查询设置的值。

到目前为止,我已经成功地使用了单个 select 下拉菜单和单个复选框,就像这样 --

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

这行得通,但对于多个值却行不通。这是我为 select 设施生成一组复选框的功能 -

<?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');
}
?>

对于 multi select 下拉列表,我使用 bootstrap multiselect,所以在我的模板上,代码如下所示 --

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

在页面上呈现为 ---

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

有什么想法吗?

更新

使用上面的代码,我的便利设施复选框在页面上呈现如下 ---

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

这与我的搜索功能一起使用,允许用户勾选要包含在查询字符串中的便利设施 --

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

我的搜索功能很广泛,但就设施的相关部分而言 -

$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'
  ));
}

更新

关于为 bootstrap multiselect 设置下拉选项,我正在尝试使用以下代码设置选项 -

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

My current search function code

Fred 在评论中的意思是这样的:

<input type="checkbox" value="condo" name="var_name[]">

name属性末尾的[]是告诉PHP应该把这个变量当作一个数组来处理。

然后在 PHP 脚本中,您可以使用类似以下内容检查数组长度:

$arr = $_POST['var_name']; echo count($arr);

根据 leith 的评论,如果它是自动生成的,那么您不必费心手动选中复选框。

相反,只需将选择的选项设置为selected,例如:

<?php
$selections = $_GET['property_type'];   // or something similar
?>
<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 ) {
        $selected = in_array($term->slug, $selections);
        echo "<option value='" . $term->slug . "' " . ($selected?' selected':'') . ">" . $term->name . "</option>";
    }
}
?>
</select>

看到这个fiddlehttps://jsfiddle.net/u1w158tp/

在其中,您只需 selected option,它会自动检查 checkboxes

创建搜索表单似乎有点倒退 - 看起来更像是为每个单独的结果生成一个表单?带注释的代码和注释如下。我尽量在我的回答中保持您的编码风格。

$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"';
    }

不过,这里的内容有些矛盾。

    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]")。

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

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

每个复选框的名称都是 name="p_am['.$am_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 (isset($_GET[$am_name])) {
        $check = 'checked';
      }

每个复选框的名称都是 name="'.$am_name.'"

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

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

附加说明 - 您还使用 $am_name 作为标签,但实际上没有在要匹配的复选框上设置 id 属性,也没有剥离非 id 字符(如括号),因此标签关联将不起作用。

      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,您只是缺少检查哪些选项是 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>

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

    // 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>";

如果您使用的是旧版本的 bootstrap select,您可能需要将 selected 字符串设为 'selected="selected"data-selected="true" ,具体取决于您使用的版本。对于当前版本,上面的字符串应该没问题。

如果 <select> 也不是一个选项数组(即不是多个),那么 [] 应该从名称中删除,并且操作类似于复选框代码:

<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> 的 HTML 实际上也不是 'work' - 它所做的只是在选项的顶部添加一个重复的 <option>列表,并且因为 nothing 是 selected,表单会将在顶部创建的副本视为 <select>.[=49 的 'selected' =]

更新 2: 搜索功能与一组复选框输入中断的原因是因为您正在对元查询使用 LIKE 比较。来自 the documentation on WordPress Meta Query:

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

该文档还展示了如何将它们连接在一起的示例。由于您没有提供实际进行搜索查询的方式,所以这有点猜测,但看起来对于一组复选框,您的代码应该类似于:

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

与文档中的示例一样,您需要确保有 relation => 'OR'relation => 'AND' 来定义您希望搜索的操作方式。

您的复选框同名。尝试添加不同的名称或使用 "p_am[]" 将它们视为数组。