duanjie2940 2012-04-25 10:08
浏览 19
已采纳

使用PHP基于POST值运行查询

I have a form that allows a user to select a county from a dropdown menu, My form is then posted to my functions.php page where I've been using IF statements to run my queries.

if ($_POST['dropdown1'] == 'option 1' && $_POST['dropdown 2'] == 'option 4' && $_POST['county'] == 'cheshire' ) {
    // RUN QUERY 
}       

My problem is however I can't realistically use IF statements for every county in every possible scenario as there will be thousands of options, has anybody got a better idea of how I can do this?


if ($_POST['vehicleType'] == 'hgv' && $_POST['coverageRegion'] == 'national' ) {        

            $customkey = 'vehicleType'; 
            $customvalue = $_POST['vehicleType']; 

            $customkey1 = 'coverageRegion'; 
            $customvalue1 = $_POST['coverageRegion']; 

            $customkey2 = 'locationType'; 
            $customvalue2 = $_POST['locationType']; $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' => '='
            ),
            array(
                'key' => $customkey2,
                'value' => $customvalue2,
                'compare' => '='
            )
            ) // end of array
            ); //end of if

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

       }
  • 写回答

1条回答 默认 最新

  • dongshao1981 2012-04-25 10:45
    关注

    You can write your code in this way so you don't have to deal with every possible option.

    global $wpdb;
    
    $postKeys = array('vehicleType', 'coverageRegion', 'locationType');
    
    $args = array(
        'orderby' => 'meta_value_num',
        'meta_key' => 'order',
        'order' => 'ASC',
        'meta_query' => array()
    );
    
    foreach ($postKeys as $key) {
        $args['meta_query'][] = array(
            'key' => $key,
            'value' => $wpdb->escape($_POST[$key]),
            'compare' => '='
        );
    }
    
    $query = new WP_Query($args);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100