2017-06-25 64 views
0

我想创建价格范围过滤器下拉菜单里同样选择选项下拉 例子:创建数组对于搜索表单选择选项下拉

<select name="price" class="filterSelectBox"> 
 
\t <option value="disable">Select Price</option> 
 
        <option value="1">$500-1000</option> 
 
        <option value="1">$1000-1500</option> 
 
        <option value="1">$1500-2000</option> 
 
        <option value="1">$2000-2500</option> 
 
        <option value="1">$2500-3000</option> 
 
</select>

我创建的PHP自定义的数组和代码像下面像上面的下拉式,但我没有得到像我想要的结果,任何一个可以解决这个问题给我吗? 在此先感谢? 我的想法是稍后在过滤中使用元查询。

<?php 
 
       $filterPrice = array(
 
        array(
 
        'maximum_price' => '1000','1500','2000','2500','3000', 
 
        'minimum_price'=> '500','1000','1500','2000','2500', 
 
        )); 
 
       ?> 
 
      <li class="filter"> 
 
       <span class="selector"> 
 
       <select name="price" class="filterSelectBox"> 
 
\t \t \t   <option value="disable"><?php _e("Select price","um_lang"); ?></option> 
 
       <?php 
 
        foreach($filterPrice as $price): 
 
       ?> 
 
        
 
        <option value="<?php echo $price['minimum_price'].'-'.$price['maximum_price'] ?>"><?php echo $price['minimum_price'].'-'.$price['maximum_price'] ?></option> 
 
        
 
       <?php 
 
       
 
       endforeach; 
 
       ?> 
 
       </select> 
 
       </span> 
 
      </li>

回答

0

这部分是奇怪:

$filterPrice = array(
    array(
     'maximum_price' => '1000','1500','2000','2500','3000', 
     'minimum_price'=> '500','1000','1500','2000','2500', 
)); 

将其更改为:

$filterPrice = array(
     'maximum_price' => array('1000','1500','2000','2500','3000'), 
     'minimum_price' => array('500','1000','1500','2000','2500'), 
); 

//..... 
foreach($filterPrice['maximum_price'] as $index=>$max_price){ 
    $min_price = $filterPrice['minimum_price'][$index]; 
    echo "<option value=".$min_price.'-'.$max_price.">".$min_price.'-'.$max_price."</option>"; 
} 
//.... 

提交后:

//... 
$filter = $_POST['price']; 
list($price_from,$price_to) = explode('-',$filter); 
//... using $price_from & $price_to for filtering 
//... 
$args = array(
    'meta_query' => array(
     'key'  => 'price', // <- meta_key name for prices 
     'value' => array($price_from, $price_to), 
     'type' => 'numeric', 
     'compare' => 'BETWEEN', 
    ), 
    // ... others query-params 
); 

$q = new WP_Query($args); 
//.... 

了解更多关于WP_Query

+0

我想创建wordpress的最低和最高价格在同一下拉内的元查询,而不是为最低和最高价格创建单独的下拉菜单。 有没有其他方法可以做到这一点?我想列出价格范围在相同的选择选项,而不是不同的下拉菜单。我想获得价值作为$ minprice和$ maxprice,所以我可以使用比较meta查询内主题。 –

+0

你使用'WP'吗?好吧,让我像回答(或@RamRaider)。然后使用'WP_Query'。 – voodoo417

+0

@Suraz Bhandari更新 – voodoo417

1

源阵列特有看着我,但我认为这会产生菜单,你想。

$filterPrice = array(
    'maximum_price' => array('1000','1500','2000','2500','3000'), 
    'minimum_price' => array('500','1000','1500','2000','2500') 
); 

$maxprices=$filterPrice['maximum_price']; 
$minprices=$filterPrice['minimum_price']; 

$html=array('<select name="price" class="filterSelectBox">'); 
foreach($maxprices as $index => $max){ 
    $min = $minprices[ $index ]; 
    $value = $min . '-' . $max; 
    $html[]="<option value='{$value}'>{$value}"; 
} 
$html[]='</select>'; 


echo implode(PHP_EOL, $html); 

如果我理解正确,那么您将需要javascript来处理菜单中选定的选项,并发送一个ajax请求,我认为。尽管说实话,我并不完全确定我了解你的确切要求。

您可以将dataset属性分配给每个选项,然后使用javascript来使用这些值。例如,如果你改变了上面的行到这个

$html[]="<option value='{$value}' data-min=$min data-max=$max>{$value}"; 

,然后加入一简单的JavaScript功能这样

$html[]=" 
<script type='text/javascript'> 
    document.querySelectorAll('select[name=\"price\"]')[0].addEventListener('change',function(e){ 
     var selected=e.target.options[ e.target.options.selectedIndex ]; 
     console.info('use these values for filtering: %s, %s', selected.dataset.min, selected.dataset.max); 
    },false); 
</script>"; 

单个最小值和最大值是经由数据集的属性可用的。希望这有所帮助。

+0

我想创建元查询为wordpres s的最小和最大价格在相同的下拉菜单中,而不是为最小和最大价格创建单独的下拉菜单。有没有其他的方法来做到这一点?我想列出价格范围在相同的选择选项,而不是不同的下拉菜单。我想获得价值作为$ minprice和$ maxprice,所以我可以使用比较meta查询内主题。 –