可能重复:
如何根据与用户输入匹配的单词数进行排名
我怎样才能让搜索引擎按输入的单词数量进行排名,并按它们在标题、描述、关键字、链接字段中出现的类型数量进行排名。
因此,较高的结果是通过拥有更多用户提交的单词来排序的。假设如果用户输入 iPhone,iPhone 关键字会出现在链接、描述和标题字段中,因此排名会高于仅在描述中提到它的 Apple 和在标题和描述中提到它的 Apple Store .
我的代码如下:
$query = " SELECT * FROM scan WHERE ";
$terms = array_map('mysql_real_escape_string', $terms);
$i = 0;
foreach ($terms as $each) {
if ($i++ !== 0){
$query .= " AND ";
}
$query .= "Match(title, description, keywords, link) Against ('".implode(' ',$terms)." IN BOOLEAN MODE') ";
}
$secs = microtime();
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error( $connect ));
echo '<p class="time">Qlick showed your results in ' . number_format($secs,2) . ' seconds.</p>';
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title1 = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
$rank = $row['rank'];
$title = substr($title1,0,60);
echo '<h2><a class="ok" href="' . $link . '">' . $title . '</a></h2>' . PHP_EOL;
echo '<p class="kk">' . $description . '<br><br><span class="keywords">' . PHP_EOL;
echo '<p><a class="okay" href="' . $link . '">' . $link . '<br><br><span class="keywords">' . PHP_EOL;
}