PHP查询基于'select'值

时间:2012-03-21 09:18:45

标签: php wordpress

我有一个包含多个值的多个下拉列表。

我正在尝试根据选择的下拉列表运行查询,因此如果只填写了1,则运行第一个查询,如果已填写2个选项,则运行第二个查询,依此类推。我确信有一个更好的方法来做到这一点,我只是不太熟悉HTML,任何帮助非常感谢......

<!-- Search query -->

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input type="hidden" name="search" value="complete">
        <select name="OPT1">
            <option value="#">Choose Your Fuel Type</option>
            <option value="petrol">Petrol</option>
            <option value="diesel">Diesel</option>
            <option value="lpg">LPG</option>
        </select>

        <select name="OPT2">
            <option value="#">Choose Your Fuel Type</option>
            <option value="petrol">Petrol</option>
            <option value="diesel">Diesel</option>
            <option value="lpg">LPG</option>
        </select>

        <select name="OPT3">
            <option value="#">Choose Your Fuel Type</option>
            <option value="petrol">Petrol</option>
            <option value="diesel">Diesel</option>
            <option value="lpg">LPG</option>
        </select>
        <input type="submit" />
    </form>



    <?php 
        if ($_POST['OPT1'] != '') {     

        echo 'option 1';         
        $pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 

        } else if ($_POST['OPT2'] != '') {               

        echo 'option 2';
        $pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' OR wpostmeta.meta_value = '".$_POST['OPT2']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 

        } 

        else if ($_POST['OPT3'] != '') {                 

        echo 'option 3';
        $pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' OR wpostmeta.meta_value = '".$_POST['OPT2']."' OR wpostmeta.meta_value = '".$_POST['OPT3']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 

        } 


    ?>

我有这个工作......

<?php 
        if ($_POST['OPT2'] == '' && $_POST['OPT3'] == '') {     

        echo 'option 1';         
        $pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 

        } else if ($_POST['OPT1'] != '' && $_POST['OPT2'] != '' && $_POST['OPT3'] == '') {               

        echo 'option 2';
        $pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' OR wpostmeta.meta_value = '".$_POST['OPT2']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 

        } 

        else if ($_POST['OPT1'] != '' && $_POST['OPT2'] != '' && $_POST['OPT3'] != '') {                 

        echo 'option 3';
        $pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = '".$_POST['OPT1']."' OR wpostmeta.meta_value = '".$_POST['OPT2']."' OR wpostmeta.meta_value = '".$_POST['OPT3']."' ORDER BY wpostmeta.meta_value DESC", OBJECT); 

        } 


    ?>

只有在拉动我的结果时,它才会偶尔拉扯......

3 个答案:

答案 0 :(得分:1)

我认为你应该从未选择的选项的选项值中删除#,即:

<option value="">Choose Your Fuel Type</option>

然后当你测试时:

$_POST['OPT1'] != ''

它应该正常工作。

如果您要检查选项1和2是否包含值,请使用:

if ($_POST['OPT1'] != '' && $_POST['OPT2'] != '') {
  // run query
}

编辑: 只需重读最后一点,你可以尝试这样的东西,看看总共输入了多少个值(不是具体哪些)你可以只有一个名为$ counter的变量并分别运行每个if语句(不使用elseif)如果语句在结尾处向计数器$counter++;添加一个,则可以看到运行if语句以查看计数器的值是0,1,2还是3.

答案 1 :(得分:0)

我认为你需要这样的东西

   if ($_POST['OPT1'] != '') {            
    $where .= "AND wpostmeta.meta_value = '".$_POST['OPT1']."'";

    }
   if ($_POST['OPT2'] != '') {               
   $where .= "wpostmeta.meta_value = '".$_POST['OPT2']."'";

    } 

    if ($_POST['OPT3'] != '') {                 
    $where .= "wpostmeta.meta_value = '".$_POST['OPT3']."'";

    }

   $pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id ".$where."' ORDER BY wpostmeta.meta_value DESC", OBJECT);

答案 2 :(得分:0)

您可以使用switch语句。

<?php
if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}
?>