2011-11-14 84 views
0

我在表達式引擎搜索中遇到了一些問題!我有一個很好的下拉式表單設置,但是我需要在此搜索表單中添加一個OPTIONAL關鍵字字段。任何想法我會如何與我當前的代碼做到這一點:Expression Engine高級搜索 - 搜索關鍵字時出現問題

主要形式代碼:

<form method="post" action="/properties/search-results/"> 

      <p>Keywords:</p> 
      <input id="keywords" type="text" name="keywords"/> 

      <p>Town:</p> 
      <select id="town" name="cat[]" multiple="multiple"> 
       <option value="" selected="selected">Any</option> 
       {exp:channel:categories channel="property" style="linear" category_group="1"} 
       <option value="{category_id}">{category_name}</option> 
       {/exp:channel:categories} 
      </select> 

      <p>Property Type:</p> 
      <select id="propertyType" name="cat[]"> 
       <option value="" selected="selected">Any</option> 
       {exp:channel:categories channel="property" style="linear" category_group="2"} 
       <option value="{category_id}">{category_name}</option> 
       {/exp:channel:categories} 
      </select> 

      <input style="margin-top:20px; width: 100px;" type="submit" name="submit" value="Search"/> 
     </form> 

搜索的結果模板:

<?php 
// Grab the categories selected from the $_POST 
// join them with an ampersand - we are searching for AND matches 
$cats = ""; 
foreach($_POST['cat'] as $cat){ 
// check we are working with a number 
if(is_numeric($cat)){ 
$cats .= $cat."&"; 
} 
} 
// strip the last & off the category string 
$cats = substr($cats,0,-1); 
?> 

{exp:channel:entries channel="property" dynamic="on" category="<?php echo($cats);?>" orderby="date" sort="asc"} 

我需要的關鍵字字段搜索{ title}我的參賽作品!

感謝您的幫助!

+0

難道沒有人知道這??? – kala233

回答

1

試試這個:先安裝Search Fields插件。 (你需要這個,因爲本地EE "search:field_name"參數僅適用於自定義字段,沒有進入冠軍。)

然後用這個修改後的代碼:

<?php 
// Grab the categories selected from the $_POST 
// join them with an ampersand - we are searching for AND matches 
$cats = array(); 
foreach($_POST['cat'] as $cat) 
{ 
    // check we are working with a number 
    if(is_numeric($cat)) 
    { 
     $cats[] = $cat; 
    } 
} 
$cats = implode('&', $cats); 

if(!empty($_POST['keywords'])) 
{ 
    $keywords = trim($_POST['keywords']); 
} 
?> 

<?php if($keywords) : ?> 
{exp:search_fields search:title="<?php echo($keywords);?>" channel="property" parse="inward"} 
<?php endif; ?> 
    {exp:channel:entries channel="property" <?php if($keywords) : ?>entry_id="{search_results}"<?php endif; ?> category="<?php echo($cats);?>" orderby="date" sort="asc"} 
     {!-- do stuff --} 
    {/exp:channel:entries} 
<?php if($keywords) : ?> 
{/exp:search_fields} 
<?php endif; ?> 
+0

嗨德里克,非常感謝,但我有一個小問題。它不會報告任何錯誤或任何內容,但是當我提交搜索表單時,它只會顯示一個空白頁面? – kala233

+0

嗨德里克,我改變了PHP解析輸出,它現在給我帶來了一個表達式引擎模板頁面,說明「搜索必須是3個字符的長度」,即使我搜索超過3個字符。 – kala233

+0

PHP一定要輸入。將index.php文件中的debug設置爲1,以查看可能出現的錯誤。 –