搜索多个关键字

时间:2011-11-09 01:16:29

标签: php

我知道,有几个问题已经像这样,我已经看过它们了。对PHP不熟悉的问题是,即使逻辑有意义,也很难将其放入自己的代码中......

我有一个搜索功能,允许用户按关键字或其他参数(下拉菜单)进行搜索。我希望他们能够搜索关键字,复数。根据其他答案,我需要使用explode / implode函数,但我不太确定何时何地实现它。

如果可能的话,如果用户决定使用逗号,我也希望没有任何故障......但获得多个关键字绝对是第一要务。

倒数第二个“if”语句适用于相关关键字。

任何帮助都是非常宝贵的。提前谢谢!

        $select = 'SELECT DISTINCT joke.id, joke.joketext, joke.jokedate, 
            author.id AS author_id, author.name AS author_name, 
            jokecategory.jokeid AS cat_jokeid, jokecategory.categoryid AS joke_catid, 
            category.id AS cat_id, category.name as cat_name, 
            joketheme.jokeid AS theme_jokeid, joketheme.themeid AS joke_themeid, theme.id
            AS theme_id, theme.name AS theme_name,
            jokegeofocus.jokeid AS geofocus_jokeid, jokegeofocus.geofocusid AS joke_geofocusid,
            geofocus.id AS geofocus_id, geofocus.name AS geofocus_name';
$from   = ' FROM joke
            inner join author on (joke.authorid = author.id)
            inner join jokecategory on (joke.id = jokecategory.jokeid)
            inner join category on (jokecategory.categoryid = category.id)
            inner join joketheme on (joke.id = joketheme.jokeid)
            inner join theme on (joketheme.themeid = theme.id)
            inner join jokegeofocus on (joke.id = jokegeofocus.jokeid)
            inner join geofocus on (jokegeofocus.geofocusid = geofocus.id)'; 
$first_where = ' where ';
$where = '';
$in = ' ORDER BY jokedate DESC';

if (is_numeric($_POST['aid']))
{   // An author is selected
    $where.= $first_where.' authorid='.$_POST['aid'];
    $first_where = ' and ';
}

if (is_numeric($_POST['cid']))
{   // A category is selected
    $where.= $first_where.' categoryid='.$_POST['cid'];
    $first_where = ' and ';
}

if (is_numeric($_POST['tid']))
{   // A theme is selected
    $where.= $first_where.' themeid='.$_POST['tid'];
    $first_where = ' and ';
}

if (is_numeric($_POST['gfid']))
{   // A region is selected
    $where.= $first_where.' geofocusid='.$_POST['gfid'];
    $first_where = ' and ';
}

if (isset($_POST['searchtext']) and $_POST['searchtext'] != '')
{   // Some search text was specified
    $where.= $first_where.' keywords LIKE "%'.(mysql_real_escape_string($_POST['searchtext'], $dbcnx)).'%"';
}

if($where == '')
{   // prevents returning the whole database, if form is empty
    $where = ' limit 20';
}
    ?>  

2 个答案:

答案 0 :(得分:0)

我建议采用以下方案:

  1. 使用jokeId,jokeKeyword
  2. 创建一个表joke_keywords
  3. 更新查询并加入新表格
  4. 使用以下内容更新代码(“LIKE”行):

    $keywords = explode(',', $_POST['searchtext']);
    
    foreach ($keywords as $key=>$value) {
        $keywords [$key]=mysql_real_escape_string(trim($value), $dbcnx);
    }
    
    $where.= ' jokeKeyword IN ("'.(implode('", "', $keywords )) . '")';
    
  5. 建议:请尝试使用现代方法作为PDO和过滤器。

答案 1 :(得分:0)

首先,正如我之前在another answer所说的那样,LIKE并不是最好的方法。您应该使用full text search

CREATE FULLTEXT INDEX keyword_fulltext_index ON joke(keywords);

其次,您可以使用函数简化代码:

function numeric_where($var, $column, &$where_array)
{
    if (isset($_POST[$var]) AND is_numeric($_POST[$var]))
    {
        array_push($where_array, $column . ' = '. $_POST[$var];
    }
}

$where_array = array();

numeric_where('aid', 'authorid', $where_array);
numeric_where('cid', 'categoryid', $where_array);
numeric_where('tid', 'themeid', $where_array);
numeric_where('gfid', 'geofocusid', $where_array);

if (isset($_POST['searchtext']) and $_POST['searchtext'] != '')
{
   $keyword_string = (mysql_real_escape_string($_POST['searchtext'], $dbcnx));
   // This is where the fulltext match happens
   array_push($where_array, "MATCH (keywords) AGAINST ('$keyword_string')");
}

if (count($where_array) > 0) 
{  // If you have any where subclauses
   $where = "WHERE " . implode($where_array, ' AND ');
}
else
{  // Else limit to 20 lines.
   $where = "LIMIT 20";
}