PHP搜索/过滤功能

时间:2012-04-25 08:56:26

标签: php mysql wordpress search filter

我的页面上有一个过滤器/搜索功能,最多包含4个下拉菜单,下拉菜单显示的内容取决于最后一次下拉菜单...

在我的下拉列表中,我有4个潜在的场景

车型

<select name="vehicleType" id="vehicleType">
    <option value="choose" selected="selected">Please choose</option>
    <option value="hgv">HGV</option>
    <option value="psv">PSV</option>
    <option value="lgv">LGV</option>
    // MORE OPTIONS AVAILABLE
</select>

覆盖范围

<select name="coverageRegion" id="coverageRegion">
    <option value="choose" selected="selected">Please choose</option>
    <option value="national">National</option>  
    <option value="international">International</option>
</select>

首选网站类型

<select name="locationType" id="locationType">
    <option value="choose" selected="selected">Please choose</option>
    <option value="truckstops">Truck stops at lowest price</option>  
    <option value="motorway">Branded motorway sites</option>
</select>

对于我的搜索功能,我目前使用'IF'语句,我说如果'vehicleType = X和Coverage Region = X'显示这些结果。

我现在需要为县添加第4个下拉菜单。在我的县里,将有大约50个县,我不能真实地为每种可能的情况都有一个IF声明,因为它需要太长时间,最好的方法是什么?

我目前正在使用我的PHP做的一个例子是......

   if ($_POST['vehicleType'] == 'car' && $_POST['pricing'] == 'pump' ) {             


             $customkey = 'vehicleType';
             $customvalue = 'car'; 

             $customkey1 = 'pricing'; 
             $customvalue1 = 'pump'; 

             $args = array('orderby' => 'meta_value_num', 'meta_key' => 'order', 'order' => 'ASC',
            'meta_query' => array(
            array(
                'key' => $customkey,
                'value' => $customvalue,
                'compare' => '='
            ),
            array(
                'key' => $customkey1,
                'value' => $customvalue1,
                'compare' => '='
            )
            )
            );

            $query = new WP_Query( $args );// The Loop
                $i = 0; $i = -1;

while ( $query->have_posts() )
{
    $i++;
    $query->the_post();
    if ( $keys = get_post_custom_keys() )
    {
        echo "<div class='clearfix card-prod ".($i==0?'first':'')."'><div class='top-dets'><span class='card-title'>";
        echo the_title();
        echo "</span>";



    // Network query 
        $network_value = get_post_custom_values('srchnetwork');
        foreach ( $network_value as $key => $value ) {
        echo '<span class="srch-val-">'. $value . '</span>'; }// Pricing Query
        $pricing_value = get_post_custom_values('srchpricing');
        foreach ( $pricing_value as $key => $value ) {
        echo '<span class="srch-val-1">'. $value . '</span>'; }

    // Setup Query
        $setup_value = get_post_custom_values('srchsetupfee');
        foreach ( $setup_value as $key => $value ) {
        echo '<span class="srch-val-2">'. $value . '</span>'; }

    // Services Query
        $services_value = get_post_custom_values('srchservices');
        foreach ( $services_value as $key => $value ) {
        echo '<span class="srch-val-3">'. $value . '</span></div>'; }

    // Big Card Query
        $bigcard_value = get_post_custom_values('bigcard');
        foreach ( $bigcard_value as $key => $value ) {
             echo '<a href="/" class="cardclick"><img src="/wp-content/themes/CAFC/images/cards/'. $value . '" alt="'; }
         echo the_title() . '" /></a>';



    echo '<img src="wp-content/themes/CAFC/images/top-choice.jpg" alt="Top Choice" class="topchoice">';
echo the_excerpt()."</div>";    

        }
}

2 个答案:

答案 0 :(得分:0)

您是否要求使用Switch语句?

我认为这不是一条路。

我假设这只是生成一个查询?使用那些参数。

可以这样做吗

$args = array('orderby' => 'meta_value_num', 'meta_key' => 'order',
'order'=>'ASC','meta_query' => array());

if (isset($_POST['vehicleType'])){
 $args['meta_query'][]=array(
        'key' => 'vehicleType',
        'value' => $_POST['vehicleType'],
        'compare' => '=');
}

if(isset($_POST['another'])){
$args['meta_query'][]=array(
        'key' => 'vehicleType',
        'value' => $_POST['vehicleType'],
        'compare' => '=');
}

 if(isset($_POST['another'])){
 $args['meta_query'][]=array(
        'key' => 'vehicleType',
        'value' => $_POST['vehicleType'],
        'compare' => '=');
}

 if(isset($_POST['another'])){
  $args['meta_query'][]=array(
        'key' => 'vehicleType',
        'value' => $_POST['vehicleType'],
        'compare' => '=');
}

如果设置了post变量(或者没有为空),基本上将变量添加到数组中

答案 1 :(得分:0)

相反,有一种简单的方法。您必须在搜索后获取密钥。把它当作值。

<option = "<?php echo $value;?>">Blah blah</option>

现在只需在此外键的基础上发送此密钥以进行更改并获取结果。