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>";
}
}
Instead there is a simple way. You must be getting a key after search. Put the value like it.
Now just send this key on change and fetch result on the basis on this foreign key.