[英]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... 我的页面上有一个过滤器/搜索功能,最多包含4个下拉列表,这些下拉列表取决于最后一个下拉列表的显示方式...
In my dropdowns I have 4 potential scenarios 在下拉菜单中,我有4种可能的情况
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. 对于我的搜索功能,我当前使用的是“ IF”语句,其中说“ vehicleType = X和Coverage Region = X”显示这些结果。
I now need to add a 4th dropdown for counties however. 现在,我需要为县添加第4个下拉列表。 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?
在我的县部分中,将有〜50个县,而我实际上无法针对每种可能的情况都使用IF语句,因为这将花费很长时间,那么最好的方法是什么?
An example of what im currently doing with my PHP is... 我目前正在使用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>";
}
}
Are you asking for a Switch statement? 您是否要提交Switch声明?
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) 如果设置了post变量(或者无论如何都不为空),基本上将变量添加到数组中
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. 现在只需发送更改密钥并基于此外键获取结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.