dongmeng2687 2011-11-09 01:16 采纳率: 0%
浏览 23

搜索多个关键字

I know, there are several questions already like this and I've looked at them all. The problem with being new to PHP is that even when the logic makes sense, it's hard to put into your own code...

I have a search feature that allows users to search by keyword or other parameters (drop down menus). I would like them to be able to search for keywords, plural. According to other answers, I need to use the explode/implode function, but I'm not quite sure when and where to implement it.

If possible, I would also like there to be no glitch if the user decides to use commas...but getting multiple keywords is definitely first priority.

The second to last "if" statement is for the keywords in question.

Any help would be invaluable. Thank you in advance!

        $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条回答 默认 最新

  • doq8211 2011-11-09 12:32
    关注

    I suggest the following scenario:

    1. create a table joke_keywords with jokeId, jokeKeyword
    2. update the query and join the new table
    3. update the code (the "LIKE" line) with something like this:

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

    An advice: please try to use modern approaches as PDO and Filter.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大