如何执行基于选择选项的PHP查询(如果条件)?

时间:2018-08-09 08:55:57

标签: php wordpress select

如何基于选择选项执行php查询(使用if条件)?

我的选择查询是

<select>
<option value="" selected disabled hidden>Choose Price</option>
<option value="1">Under 50000</option>
<option value="2">50000 to 100000</option>
<option value="3">100000 to 150000</option>
</select>

这是我的php代码

<?php 
$my_query = new WP_query();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page'=> 6,'post_type'=> 'page', 'paged' => $paged, 'order'=> 'DESC',
'meta_query' => array(
    array(
    'key'=> 'wpcf-just-launched-bikes',
    'value'     => '1',
    ),
),
);
$my_query->query($args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
?> 

如果我选择第一个选项(低于50000),请执行上述php代码
如果我选择第二个选项,我想同样执行另一个php代码

(注意:如有条件,请更好地使用)

2 个答案:

答案 0 :(得分:0)

我认为您想要此查询:

SELECT SQL_NO_CACHE
    n.number, n.name, 
MAX(CASE WHEN f1.field_name='age' THEN nf1.value END) as Age,
MAX(CASE WHEN f1.field_name='email' THEN nf1.value END) as Email,
MAX(CASE WHEN f1.field_name='gender' THEN nf1.value END) as Gender
FROM
    number AS n
        LEFT JOIN
    number_field AS nf1 ON n.id = nf1.number_id
        RIGHT JOIN
    field AS f1 ON f1.id = nf1.field_id
WHERE`enter code here`
    1 = 1
GROUP BY n.number,n.name
ORDER BY number

这只是我工作的一个例子。但这对您很有用。

答案 1 :(得分:0)

您是否在查询页面中传递价格(选择)值? 如果是的话:

<?php
$price = pricevalue; / store select value here
$my_query = new WP_query();
if($price == 2){
    // query or your code
}elseif($price == 3){
    // query or your code
}else{
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array('posts_per_page'=> 6,'post_type'=> 'page', 'paged' => $paged, 'order'=> 'DESC',
    'meta_query' => array(
    array(
    'key'=> 'wpcf-just-launched-bikes',
    'value'     => '1',
    ),
    ),
);
}
$my_query->query($args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();

// after endif  
wp_reset_postdata(); // add this code
?>