2011-11-09 192 views
0

我知道,有几个问题已经这样,我已经看了他们所有。 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'; 
} 
    ?> 
+0

我给这里同样的答案我在另一个地方做的:http://stackoverflow.com/questions/8035485/如何在一个字符串存储在myadmin数据库和搜索一个单词从那个st/8035688#8035688 –

回答

0

我建议以下情形:

  1. 创建jokeId表joke_keywords,jokeKeyword
  2. 更新查询,并加入了新表
  3. 更新的代码(以下简称 “LIKE”线),如下所示:

    $keywords = explode(',', $_POST['searchtext']); 
    
    foreach ($keywords as $key=>$value) { 
        $keywords [$key]=mysql_real_escape_string(trim($value), $dbcnx); 
    } 
    
    $where.= ' jokeKeyword IN ("'.(implode('", "', $keywords)) . '")'; 
    

建议:请尝试使用现代方法作为PDO和过滤器。

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"; 
} 
+0

嗨古斯塔夫, 你的代码看起来像它应该工作,但我我无法创建全文索引。显然InnoDB不支持这一点。有没有办法解决这个问题或者使用InnoDB获得相同结果的另一种方法?谢谢! – user1017566

+0

MySQL仅支持MyISAM表上的全文索引。您应该能够将一个表切换到不同的存储引擎,而不会产生任何可怕的后果。然而,如果你必须保持它作为InnoDB,那么你应该去看看这个问题:[InnoDB的全文搜索](http://stackoverflow.com/questions/1381186/fulltext-search-with-innodb) –