【问题标题】:Expression Engine Advanced Search - Problems searching for keyword表达式引擎高级搜索 - 搜索关键字时出现问题
【发布时间】:2011-11-14 16:25:41
【问题描述】:

我在搜索表达式引擎时遇到了一点问题!我有一个很好的下拉表单设置,但是我需要在这个搜索表单中添加一个可选的关键字字段。任何想法我将如何使用我当前的代码做到这一点:

主窗体代码:

<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}!

感谢您的帮助!

【问题讨论】:

    标签: php html search expressionengine


    【解决方案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; ?>
    

    【讨论】:

    • 嗨 Derek, 非常感谢,但是我遇到了一点问题。它不报告任何错误或任何东西,但是当我提交搜索表单时,它只是弹出一个空白页面?
    • 嗨 Derek,我将 PHP 解析更改为输出,现在它给我带来了一个表达式引擎模板页面,说明“搜索长度必须为 3 个字符”,即使我正在搜索超过 3 个字符。
    • PHP 绝对应该在输入中。在 index.php 文件中将 debug 设置为 1 以查看可能出现的错误。
    • 嗨 Derek,现在收到错误:解析错误:语法错误,/home/inc_marketing/campbellcairns.com/admin/expressionengine/libraries/Functions.php(656) 中出现意外的“}”:eval () 的代码在第 11 行
    • Ops,我那里少了一个分号。应该是$cats[] = $cat;。尝试修复它,看看会发生什么。
    猜你喜欢
    • 2011-10-13
    • 1970-01-01
    • 2016-12-22
    • 2010-11-15
    • 2012-08-03
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多