2017-06-25 63 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查詢內主題。 –