0

我正在建立一个根据酒店类型、关键字、位置和价格搜索酒店的网站。除了排序,一切都很好。我可以在显示结果之前对结果进行排序;但是,在它们显示后,我无法对它们进行重新排序。

注意:我正在使用来自 TEMPLATIC 的房地产 WORDPRESS 主题。

这是从 db 中检索记录并根据 $sort 类型值按价格或日期对它们进行排序的 php 代码。

<?php   
$sort type=0; //0-sort by price ... 1-sort by date
...
if ($sort_type == 0) {
$srch_sql .= "  order by abs(o.meta_value) asc limit $startlimit , $posts_per_page";
}
elseif ($sort_type == 1){
$srch_sql .= "  order by p.id desc limit $startlimit , $posts_per_page";
}
?>

我知道至少应该有一个解决方案。我现在正在考虑的一个是有两个单选按钮。一种用于按日期排序,另一种用于按价格排序。

<input type="radio" name="sort_type" value="Price">Price</input>
<input type="radio" name="sort_type" value="Date">Date</input>

但是,这个问题是我不知道如何在选择从一个单选按钮更改为另一个单选按钮并重新加载页面而不将变量 $sort_type 重置为时将值(1 和 0)分配给变量 $sort_type重新加载页面时为 0。

有什么建议!?

下面的整个代码

<?php get_header(); ?>
<?php if (is_paged()) $is_paged = true; ?>

<div class="wrapper" >
    <div class="clearfix container_border">
           <div class="breadcrumbs">
            <p><?php if ( get_option( 'ptthemes_breadcrumbs' )) { yoast_breadcrumb('',''); } ?></p>
                <span class="findproperties" onclick="show_hide_propertysearchoptions();"><a href="javascript:void(0);"><?php _e(FIND_PROPERTIES_TEXT);?></a></span>
            </div>
        </div>

<?php require_once (TEMPLATEPATH . '/library/includes/search.php');  ?>




 <?php  
    //sort_type = 0 order by price
    //sort_type = 1 order by date
    $sort_type = 0; ?>


     // this part of the code retrieves data from db
 <?php  
    $is_search = 0;
    global $wpdb;
    $totalpost_count = 0;
    $propertycategory = get_cat_id_from_name(get_option('ptthemes_propertycategory'));
    $propertycategorys = get_sub_categories($propertycategory,'string');
    $all_pids_arr = $wpdb->get_var("SELECT group_concat(ID) FROM $wpdb->posts where post_status='publish'");
    $all_pids_arr = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_taxonomy tt join $wpdb->term_relationships tr on tr.term_taxonomy_id=tt.term_taxonomy_id where tt.term_id in ($propertycategorys)");
    if($_REQUEST['srch_location'])
    {
        $is_search = 1;
        $srch_location = $_REQUEST['srch_location'];
        $location_pids_arr = $wpdb->get_col("select post_id from $wpdb->postmeta where meta_key like 'add_location' and meta_value like \"$srch_location\"");
        $all_pids_arr = array_intersect($all_pids_arr,$location_pids_arr);
    }
    if($_REQUEST['srch_price'])
    {
        $is_search = 1;
        $srch_price = $_REQUEST['srch_price'];
        if(strstr($srch_price,'-'))
        {
            $srch_price_str = str_replace('-',' and ',$srch_price);
            $srch_price_arr = $wpdb->get_col("select post_id from $wpdb->postmeta where meta_key like 'price' and meta_value between $srch_price_str");
        }
        elseif(strstr($srch_price,'+'))
        {
            $srch_price_str = str_replace('+','',$srch_price);
            $srch_price_arr = $wpdb->get_col("select post_id from $wpdb->postmeta where meta_key like 'price' and meta_value >= $srch_price_str");
        }           
        $all_pids_arr = array_intersect($all_pids_arr,$srch_price_arr);
    }

    if($_REQUEST['srch_type'])
    {
        $is_search = 1;
        $srch_type = $_REQUEST['srch_type'];
        $type_pids_arr = $wpdb->get_col("select post_id from $wpdb->postmeta where meta_key like 'property_type' and meta_value = \"$srch_type\"");
        $all_pids_arr = array_intersect($all_pids_arr,$type_pids_arr);
    }

    if($_REQUEST['srch_keyword'] && $_REQUEST['srch_keyword']!=CITY_STATE_ZIP_SRCH_TEXT)
    {
        $is_search = 1;
        $srch_keyword = $_REQUEST['srch_keyword'];

        $kw_pids_arr = $wpdb->get_col("select id from $wpdb->posts where post_title like \"%$srch_keyword%\"");

        $all_pids_arr = array_intersect($all_pids_arr,$kw_pids_arr);
    }

    if($is_search && !$all_pids_arr)
    {
        $all_pids_arr[0] = 'nopost';
    }
    if($_REQUEST['srch_property_id'])
    {
        $post_ids_str = $_REQUEST['srch_property_id'];
        $sub_cat_sql .= " and p.ID in ($post_ids_str) ";
    }else
    {
        if($all_pids_arr)
        {
            $post_ids_str = implode(',',$all_pids_arr);
            if($post_ids_str)
            {
                $sub_cat_sql .= " and p.ID in ($post_ids_str) ";
            }
        }   
    }
    $featurecat = get_cat_id_from_name(get_option('ptthemes_featuredcategory'));
    if($featurecat)
    {
        $srch_feature_pids = $wpdb->get_var("SELECT group_concat(tr.object_id) FROM $wpdb->term_taxonomy tt join $wpdb->term_relationships tr on tr.term_taxonomy_id=tt.term_taxonomy_id where tt.term_id in ($featurecat)");
        $srch_feature_pids = '';
    }
    $blogcat = get_cat_id_from_name(get_option('ptthemes_blogcategory'));
    $blogcatcatids = get_sub_categories($blogcat,'string');
    if($blogcatcatids)
    {
        $srch_blog_pids = $wpdb->get_var("SELECT group_concat(tr.object_id) FROM $wpdb->term_taxonomy tt join $wpdb->term_relationships tr on tr.term_taxonomy_id=tt.term_taxonomy_id where tt.term_id in ($blogcatcatids)");
    }
    if($srch_blog_pids && $srch_feature_pids)
    {
        $sub_cat_sql .= " and p.ID not in ($srch_blog_pids,$srch_feature_pids) ";
    }elseif($srch_blog_pids && $srch_feature_pids=='')
    {
        $sub_cat_sql .= " and p.ID not in ($srch_blog_pids) ";
    }elseif($srch_blog_pids=='' && $srch_feature_pids)
    {
        $sub_cat_sql .= " and p.ID not in ($srch_feature_pids) ";
    }

    $srch_sql = "select p.* from $wpdb->posts p join $wpdb->postmeta o on p.ID=o.post_id $post_meta_join where p.post_status='publish' and p.post_type='post' and o.meta_key='price'  $sub_cat_sql";
    if($srch_feature_pids)
    {
        $feature_srch_sql = "select p.* from $wpdb->posts p join $wpdb->postmeta o on p.ID=o.post_id $post_meta_join where p.post_status='publish' and p.post_type='post' and o.meta_key='price' and p.ID in ($srch_feature_pids)";
        $srch_sql = " select * from (($feature_srch_sql) union ($srch_sql))";
    }

    $totalpost_count = $wpdb->get_var("select count(p.ID) from $wpdb->posts p join $wpdb->postmeta o on p.ID=o.post_id $post_meta_join where p.post_status='publish' and p.post_type='post' and o.meta_key='price'  $sub_cat_sql");
    global $posts_per_page,$paged;
    if($paged==''){$paged=1;}
    $startlimit = $posts_per_page*($paged-1);

    if ($sort_type == 0) {
    $srch_sql .= "  order by abs(o.meta_value) asc limit $startlimit , $posts_per_page";
    }
    elseif ($sort_type == 1){
    $srch_sql .= "  order by p.id desc limit $startlimit , $posts_per_page";
    }

    $post_info = $wpdb->get_results($srch_sql);

    ?><!-- contentarea #end --> 



<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/library/js/jquery.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/library/js/functions.js"></script>

<div class="contentarea">
<div class="content" >
<div class="latestproperties latestproperties_<?php echo stripslashes(get_option('ptthemes_sidebar_left'));  ?>">



<p align="right">Order by:
<input type="radio" name="order" value="Price" />Price</input>
<input type="radio" name="order" value="Date" />Date</input></p>

    <h5><span><a href="#" class="switch_thumb"><?php _e(SWITCH_THUMB_TEXT);?></a></span>

    <?php
    if($_REQUEST['s'] == 'shfleto te gjitha')
    {
        _e(LATEST_PROPERTIES_BYPRICE_TEXT);
    }
    elseif(is_category() && $_REQUEST['search']=='')
    {
        echo single_cat_title();
    }else
    {
        echo get_search_param();
    }   
    ?></h5>     


    <?php if($post_info) { ?>               
    <ul class="display ">
    <?php
    $count=0;
    foreach($post_info as $post_info_obj)
    {
        $count++;
        $post = $post_info_obj;
        get_property_info_li($post);
        if($count%3==0)
        {
        ?>
        <li class="blank"></li>
        <?php   
        }
    }
    ?>
    </ul>
    <?php
        }else
        {
            _e(NO_PROPERTY_AVAILABLE_MSG);
            if($_POST['search']=='search')
            {
                echo get_search_param();
            }
        }
        ?>
    <?php if($post_info) { ?>
    <div class="pagination">
<?php if (function_exists('wp_pagenavi')) { ?><?php wp_pagenavi(); ?><?php } ?>
     </div>
    <?php }?>
</div>

<?php get_sidebar(); ?>  <!-- sidebar #end -->
</div>

<?php get_footer(); ?>
4

1 回答 1

0

我会使用 jquery 在单击单选按钮时将数据加载到页面上的 div 中。像这样的东西:

<input type='radio' onselect='showResults("date")'/><br/>
<input type='radio' onselect='showResults("price")'/><br/>

<script type='text/javascript'>
    function showResults(sortField){
        $('#results_container').load('results_page.php?sort='+sortField);

    }
</script>


<div id='results_container'></div>
于 2012-07-21T19:50:19.603 回答