0

我正在玩 PHPFreaks“简单 SQL 搜索”脚本。我想知道在提交查询后如何优化搜索方法。

假设我提交了一个像“音乐”这样的搜索词,搜索查询会给我数据库中包含“音乐”这个词的所有数据的结果,但是假设我想搜索“音乐”这个词,一旦结果已显示我想优化搜索并查找音乐下属于“摇滚”或“说唱”类别的所有结果。我想使用选定选项列表来执行此操作。

这是来自 PHPFreaks 的工作脚本,我将其包含在内,因此这里的用户也可以在需要时使用此脚本:

$error = array();
$results = array();


if (isset($_GET['search'])) {
   $searchTerms = trim($_GET['search']);
   $searchTerms = strip_tags($searchTerms); // remove any html/javascript.

   if (strlen($searchTerms) < 3) {
      $error[] = "Search terms must be longer than 3 characters.";
   }else {
      $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
   }

   // If there are no errors, lets get the search going.
   if (count($error) < 1) {
      $searchSQL = "SELECT sid, sbody, stitle, sdescription FROM simple_search WHERE ";

      // grab the search types.
      $types = array();
      $types[] = isset($_GET['body'])?"`sbody` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['title'])?"`stitle` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['desc'])?"`sdescription` LIKE '%{$searchTermDB}%'":'';

      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)

      if (count($types) < 1)
         $types[] = "`sbody` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked

          $andOr = isset($_GET['matchall'])?'AND':'OR';
      $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `stitle`"; // order by title.

      $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");

      if (mysql_num_rows($searchResult) < 1) {
         $error[] = "The search term provided {$searchTerms} yielded no results.";
      }else {
         $results = array(); // the result array
         $i = 1;
         while ($row = mysql_fetch_assoc($searchResult)) {
            $results[] = "{$i}: {$row['stitle']}<br />{$row['sdescription']}<br />{$row['sbody']}<br /><br />";
            $i++;
         }
      }
   }
}

function removeEmpty($var) {
   return (!empty($var)); 
}
?>

结果:

 <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>
      <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
         Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><br />
         Search In:<br />
         Body: <input type="checkbox" name="body" value="on" <?php echo isset($_GET['body'])?"checked":''; ?> /> | 
         Title: <input type="checkbox" name="title" value="on" <?php echo isset($_GET['title'])?"checked":''; ?> /> | 
         Description: <input type="checkbox" name="desc" value="on" <?php echo isset($_GET['desc'])?"checked":''; ?> /><br />
                 Match All Selected Fields? <input type="checkbox" name="matchall" value="on" <?php echo isset($_GET['matchall'])?"checked":''; ?><br /><br />
         <input type="submit" name="submit" value="Search!" />
      </form>
      <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>

我希望有人能在这里引导我朝着正确的方向前进。我对 PHP/MySQL 有点熟悉,并且到处都在网上查看,但无法找到使用此脚本优化搜索结果的方法。谢谢。

4

1 回答 1

0

您有两个选项来优化搜索结果。

  1. 将搜索(生成的行集)保存/序列化到磁盘/会话,以便在用户向搜索参数添加任何内容时进行搜索。在这种情况下,您需要确保用户没有更改“基本”搜索参数。如果他这样做了,您可能会先运行另一个基础搜索,然后从结果中提取您的精炼结果(特定类型的条目)。

  2. 将新的搜索参数添加到一些基本查询字符串,并让数据库进行精确搜索。

在这两种情况下,您最终都会得到一个精炼的结果。我会选择第二个选项,您不需要关心以前的搜索参数。您只需将“流派”添加到查询中,您就应该完成了。

于 2013-04-08T13:57:15.860 回答