我知道,有几个问题已经这样,我已经看了他们所有。 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';
}
?>
我给这里同样的答案我在另一个地方做的:http://stackoverflow.com/questions/8035485/如何在一个字符串存储在myadmin数据库和搜索一个单词从那个st/8035688#8035688 –