多条件筛选过滤(初级)


cms :pages标签(Couch 用于列出页面的主力)也支持使用其“custom_field”参数根据页面可编辑区域的内容查找页面。

例如,如果一个房产列表网站的房产模板中包含以下自定义字段:

地点
房产类型(例如商业、住宅等)
价格
卧室数量
浴室数量
占地面积
以下代码将获取所有商业地产条目,其中卧室数量为 5 间或更多,占地面积为 7000 个单位或更多。

<cms:pages  custom_field="property_type==Commercial | num_of_bedrooms>=5 | plot_area>=7000"  >
   ...
</cms:pages>

当然,上面代码片段中的“ filter ”条件是硬编码的,因此无法帮助您完全实现目标。
但是,如果我们能够以某种方式使用访问者的输入动态地拼凑我们输入到custom_field参数中的值,那么我们就可以了。
为了演示如何做到这一点,我整理了一个小型且功能齐全的房产模板。它没有样式,以便只关注当前主题。
附加的2个文件代码:“property.php”模板和它使用的名为“search.htm”的代码片段。
请将“property.php”模板放在您的根文件夹中,将“search.htm”代码片段放在“couch/snippets”文件夹(或您在 config.php 中指定的任何代码片段文件夹)中。

模板“property.php”定义了几个与房产相关的可编辑区域(例如价格、面积等)。
“search.htm”代码片段会创建一个表单,显示这些参数,并允许访问者指定自己的选择。
提交后,所选的值将用于生成一个“custom_field”参数接受的格式的搜索字符串,然后该搜索字符串会被输入到 cms:pages 标签循环中,以提取匹配的页面。

添加一些属性,并在前端测试搜索表单。

property.php代码:

<?php require_once( 'couch/cms.php' ); ?>
   <cms:template title='Property' clonable='1' >
      <cms:editable name="my_intro" label="Introduction" type="textarea" />
      <cms:editable name="my_addr" label="Address" height='70' type="textarea" />
      <cms:editable name="my_locality" label="Locality" desc="Select one from these"
                    opt_values='Arera Colony  | M P Nagar | Old City | Karond'
                    opt_selected = 'Old City'
                    required='1'
                    type="dropdown" />
      <cms:editable name="my_property_type" label="Property Type" desc="Select one from these"
                    opt_values='Residential  | Commercial | Rental'
                    required='1'
                    type="dropdown"/>
      <cms:editable name="my_price" label="Price" desc='Amount in Rupee\'s (correct upto 2 decimal points)'
                    search_type='decimal'
                    validator='non_zero_decimal'
                    required='1'
                    type="text"/>        
      <cms:editable name="my_num_bedrooms" label="Number Of Bedrooms" 
                    search_type='integer'
                    validator='non_negative_integer'
                    type="text">0</cms:editable>
      <cms:editable name="my_num_bathrooms" label="Number Of Bathrooms" 
                    search_type='integer'
                    validator='non_negative_integer'
                    type="text">0</cms:editable>        
      <cms:editable name="my_plot_area" label="Plot Area" desc="Plot area in sq.ft (correct upto 2 decimal points)"
                    search_type='decimal'
                    validator='non_zero_decimal'
                    required='1'
                    type="text"/>    
   </cms:template>
   <cms:if k_is_page >
      <!-- show single property detail here -->
   <cms:else />
   
      <!-- List view (shows search) -->
      <cms:embed 'search.htm' />
      Searching for:<cms:show my_search_str /><br /><!-- for debugging -->
<cms:pages paginate='1' limit='10' custom_field="<cms:show my_search_str />" orderby='my_price' >
         <cms:if k_paginated_top >
            <cms:set my_records_found='1' scope='global'/>
            <cms:if k_paginator_required > 
               Page <cms:show k_current_page /> of <cms:show k_total_pages /><br />
            </cms:if>
            <cms:show k_total_records /> Properties Found. (Displaying: <cms:show k_record_from />-<cms:show k_record_to />)
         </cms:if>
         <h2><cms:show k_page_title /></h2> 
<cms:paginator simple='1' position='1' />
</cms:pages>      
<cms:if my_records_found!='1' >
         No properties Found
      </cms:if>   
</cms:if>
<?php COUCH::invoke(); ?>

search.html 代码

<div id="quick-search" style="float:right; margin-right:50px;">
    <cms:form name="quicksearch" id="quicksearch" anchor='0'>
        <cms:if k_success >
            
            <cms:if frm_locality!='-' >
                <cms:set my_search_str="<cms:concat my_search_str ' | my_locality==' frm_locality />"  scope='global'/>
            </cms:if>
            
            <cms:if frm_property_type!='-' >
                <cms:set my_search_str="<cms:concat my_search_str ' | my_property_type==' frm_property_type />"  scope='global'/>
            </cms:if>
            
            <cms:if frm_min_price!='-' >
                <cms:set my_search_str="<cms:concat my_search_str ' | my_price>=' frm_min_price />"  scope='global'/>
            </cms:if>    
            
            <cms:if frm_max_price!='-' >
                <cms:set my_search_str="<cms:concat my_search_str ' | my_price<=' frm_max_price />"  scope='global'/>
            </cms:if>            

            <cms:if frm_min_bedrooms!='-' >
                <cms:set my_search_str="<cms:concat my_search_str ' | my_num_bedrooms>=' frm_min_bedrooms />"  scope='global'/>
            </cms:if>

            <cms:if frm_min_bathrooms!='-' >
                <cms:set my_search_str="<cms:concat my_search_str ' | my_num_bathrooms>=' frm_min_bathrooms />"  scope='global'/>
            </cms:if>

            <cms:if frm_min_sqft!='-' >
                <cms:set my_search_str="<cms:concat my_search_str ' | my_plot_area>=' frm_min_sqft />"  scope='global'/>
            </cms:if>
            
        </cms:if>
        
        <div>
            <label>Locality</label>
            <cms:input type="dropdown" 
                    opt_values='No Preference=- | Arera Colony  | M P Nagar | Old City | Karond'
                    opt_selected='-'
                    name="locality" id="locality" />
        </div>
        <div>
            <label>Property Type</label>
            <cms:input type="dropdown" 
                    opt_values='No Preference=- | Residential  | Commercial | Rental'
                    opt_selected='No Preference'
                    name="property_type" id="property_type" />
        </div>
        <div>
            <label>Minimum Price</label>
            <cms:input type="dropdown" 
                    opt_values='
                    No Minimum=- | Rs. 100,000=100000  | Rs. 125,000=125000 | Rs. 150,000=150000
                    | Rs. 175,000 = 175000 | Rs. 200,000=200000  | Rs. 225,000=225000 | Rs. 250,000=250000
                    | Rs. 275,000 = 275000 | Rs. 300,000=300000  | Rs. 325,000=325000 | Rs. 350,000=350000
                    | Rs. 375,000 = 375000 | Rs. 400,000=400000  | Rs. 425,000=425000 | Rs. 450,000=450000
                    | Rs. 475,000 = 475000 | Rs. 500,000=500000  | Rs. 600,000=600000 | Rs. 700,000=700000
                    | Rs. 800,000 = 800000 | Rs. 900,000=900000  | Rs. 10,00,000=1000000 | Rs. 12,50,000=1250000
                    | Rs. 15,00,000 = 1500000 | Rs. 17,50,000=1750000  | Rs. 20,00,000=2000000 | Rs. 22,50,000=2250000
                    | Rs. 25,00,000 = 2500000 | Rs. 27,50,000=2750000  | Rs. 30,00,000=3000000 | Rs. 32,50,000=3250000
                    | Rs. 35,00,000 = 3500000 | Rs. 37,50,000=3750000  | Rs. 40,00,000=4000000 | Rs. 45,00,000=4500000
                    | Rs. 50,00,000 = 5000000 | Rs. 55,00,000=5500000  | Rs. 60,00,000=6000000 | Rs. 65,00,000=6500000
                    | Rs. 70,00,000 = 7000000 | Rs. 75,00,000=7500000  | Rs. 80,00,000=8000000 | Rs. 85,00,000=8500000
                    | Rs. 90,00,000 = 9000000 | Rs. 95,00,000=9500000  | Rs. 100,00,000=10000000
                    '
                    opt_selected='-'
                    name="min_price" id="min_price" />
        </div>
        <div>
            <label>Maximum Price</label>
            <cms:input type="dropdown" 
                    opt_values='
                    No Maximum = - | Rs. 100,000=100000  | Rs. 125,000=125000 | Rs. 150,000=150000
                    | Rs. 175,000 = 175000 | Rs. 200,000=200000  | Rs. 225,000=225000 | Rs. 250,000=250000
                    | Rs. 275,000 = 275000 | Rs. 300,000=300000  | Rs. 325,000=325000 | Rs. 350,000=350000
                    | Rs. 375,000 = 375000 | Rs. 400,000=400000  | Rs. 425,000=425000 | Rs. 450,000=450000
                    | Rs. 475,000 = 475000 | Rs. 500,000=500000  | Rs. 600,000=600000 | Rs. 700,000=700000
                    | Rs. 800,000 = 800000 | Rs. 900,000=900000  | Rs. 10,00,000=1000000 | Rs. 12,50,000=1250000
                    | Rs. 15,00,000 = 1500000 | Rs. 17,50,000=1750000  | Rs. 20,00,000=2000000 | Rs. 22,50,000=2250000
                    | Rs. 25,00,000 = 2500000 | Rs. 27,50,000=2750000  | Rs. 30,00,000=3000000 | Rs. 32,50,000=3250000
                    | Rs. 35,00,000 = 3500000 | Rs. 37,50,000=3750000  | Rs. 40,00,000=4000000 | Rs. 45,00,000=4500000
                    | Rs. 50,00,000 = 5000000 | Rs. 55,00,000=5500000  | Rs. 60,00,000=6000000 | Rs. 65,00,000=6500000
                    | Rs. 70,00,000 = 7000000 | Rs. 75,00,000=7500000  | Rs. 80,00,000=8000000 | Rs. 85,00,000=8500000
                    | Rs. 90,00,000 = 9000000 | Rs. 95,00,000=9500000  | Rs. 100,00,000=10000000 
                    '
                    opt_selected='-'
                    name="max_price" id="max_price" />

        </div>
        <div>
            <label>Bedrooms </label>
            <cms:input type="dropdown" 
                    opt_values='No Preference=- | 1 Bed=1  | 2 Bed=2 | 3 Bed=3
                    | 4 Bed=4  | 5 Bed=5 | 6 Bed=6 | 7 Bed=7
                    '
                    opt_selected='No Preference'
                    name="min_bedrooms" id="min_bedrooms" />
             (minimum)        
        </div>
        <div>
            <label>Bathrooms</label>
            <cms:input type="dropdown" 
                    opt_values='No Preference=- | 1  | 2 | 3 
                    | 4  | 5 | 6 | 7 | 8 
                    '
                    opt_selected='No Preference'
                    name="min_bathrooms" id="min_bathrooms" />    
            (minimum)        
        </div>
        <div>
            <label>Square Feet</label>
            <cms:input type="dropdown" 
                    opt_values='
                    No Preference = - | 500 sq. ft=500  | 1,000 sq. ft=1000 | 1,500 sq. ft=1500
                    | 2,000 sq. ft=2000  | 2,500 sq. ft=2500 | 3,000 sq. ft=3000 | 3,500 sq. ft=3500
                    | 4,000 sq. ft=4000  | 4,500 sq. ft=4500 | 5,000 sq. ft=5000 | 5,500 sq. ft=5500
                    | 6,000 sq. ft=6000  | 6,500 sq. ft=6500 | 7,000 sq. ft=7000 | 7,500 sq. ft=7500
                    | 8,000 sq. ft=8000  | 8,500 sq. ft=8500 | 9,000 sq. ft=9000 | 9,500 sq. ft=9500
                    | 10,500 sq. ft=10500
                    '
                    opt_selected='-'
                    name="min_sqft" id="min_sqft" />
            (minimum)
        </div>
        <div class="buttonset">
            <cms:input type="submit" class="fbsubmitbtn" value="Start Searching!" name="submit"/>
        </div>
    </cms:form>
</div>