使用带复选框的ISSET

问题描述 投票:4回答:3

我正在使用wordpress搜索表单来优化当前搜索,我想要做的是搜索结果页面的搜索,并根据查询设置值。

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

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

这是有效的,但对于多个值,它没有。这是我生成一组复选框以选择设施的功能 -

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

对于多选下拉列表我使用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>

有任何想法吗?

UPDATE

使用我的设施上面的代码复选框在页面上显示如此---

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

UPDATE

关于设置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>"; 
?>

我目前的搜索功能code

php wordpress forms checkbox isset
3个回答
5
投票

这对于创建搜索表单似乎有点落后 - 看起来更像是为每个单独的结果生成一个表单?下面带注释的代码和注释。在我的回答中,我试图保持你的编码风格。

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

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

      $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中的数组,那么您只需使用循环名称(aazity)($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 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"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实际上并不“正常” - 所有它正在做的是在选项列表的顶部添加一个重复的<option>,并且由于没有选择任何内容,表单将把顶部创建的副本视为被选中为<select>

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

只有当比较为“IN”,“NOT IN”,“BETWEEN”或“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'来定义搜索的运行方式。


2
投票

弗雷德在评论中的意思是这样的:

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

看到这个小提琴https://jsfiddle.net/u1w158tp/

在它你只是selected options它会自动检查checkboxes


-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.