简体   繁体   中英

PHP Search/Filter function

I have a filter/search function on my page that consists of a maximum of 4 dropdowns, the dropdowns show dependant on what the last dropdown was...

In my dropdowns I have 4 potential scenarios

Vehicle Type

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

Coverage Region

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

Type of Site Preferred

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

For my search function im currently using 'IF' statements where I say if 'vehicleType = X and Coverage Region = X' show these results.

I now need to add a 4th dropdown for counties however. In my counties section there will be ~50 counties and I cant realistically have an IF statement for every possible scenario as it will take far too long, What the best way of doing this?

An example of what im currently doing with my PHP is...

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

        }
}

Are you asking for a Switch statement?

I don't think thats the way to go.

I'm assuming this just generates a query? using those paramaters.

Could it be done like this

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

Basically adding the variables to your array if the post variable is set (or not empty whatever)

Instead there is a simple way. You must be getting a key after search. Put the value like it.

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

Now just send this key on change and fetch result on the basis on this foreign key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM