我已经整理了一个迷你搜索算法,它根据他们输入的内容以及测试正文中的链接数量、垃圾邮件等因素为搜索结果分配分数。
不幸的是,我不知道如何根据这个数字生成的结果来排序结果。关于在分配给它的“分数”上排序 PHP 结果的任何想法?我应该补充一点,这个分数并不保存在数据库中,而是每次都根据用户搜索的内容生成。
好的,所以这会根据使用的搜索词按顺序列出所有结果,我需要它按“TotalScore”排序,但我不知道该怎么做......
这是代码:
$refType = $_GET["ref"];
$setLimit = $_GET["list"];
//CATCH & SECURE THE QUERY
$q = mysql_real_escape_string($_GET["q"]);
//SET LISTING VALUE IF CHOSEN, DEFAULT 10
if(!$setLimit || $setLimit == "0"){
$setLimit = "10";
} else {
$setLimit = $_GET["list"];
}
//GET NUMBER OF WORDS IN QUERY & RETURN ERROR IF MORE THAN 12
$searchTermCount = str_word_count($q);
if($searchTermCount > 12){
$searchLine = "";
$searchList = "Your search contained too many words! Please go back and try again, use less than 12 words to find what you are looking for.";
} else if($q == "" || !$q || $q == " "){
$searchLine = "";
$searchList = "You did not submit a search term! Please go back and enter your city into the search bar!";
} else {
$searchLine = "Your search for " . $q . " found the following results";
//PUT NEW ALGORITHM HERE
$findDetails = mysql_query("SELECT * FROM [TABLENAME] WHERE upper(city) like '%$q%' AND verified='1' OR upper(county) like '%$q' AND verified='1' OR upper(title) like '%$q%' AND verified='1' OR upper(intro) like '%$q%' AND verified='1' OR upper(content) like '%$q%' AND verified='1' ORDER BY datePosted DESC LIMIT $setLimit");
while($row = mysql_fetch_array($findDetails)){
$adId = $row["id"];
$adTitle = $row["title"];
$adIntro = $row["intro"];
$adCity = $row["city"];
$adCounty = $row["county"];
$adVerified = $row["verified"];
$cutIntro = substr($adIntro, 0, 140);
$numberOfResults = mysql_num_rows($findDetails);
$findReviews = mysql_query("SELECT * FROM [TABLENAME2] WHERE adventureID='$adId'");
$findReviews = mysql_num_rows($findReviews);
$titleScore = "";
$introScore = "";
$contentScore = "";
//CUT SEARCH TERM
$qBreak = explode(" ", $q);
foreach($qBreak as $qWord){
$titleScore = $titleScore+substr_count($adTitle, $qWord);
$introScore = $introScore+substr_count($adIntro, $qWord);
$contentScore = $contentScore+substr_count($adContent, $qWord);
}
$totalScore = $titleScore+$introScore+$contentScore;
$searchList .='
<div style="width: 100%;" class="result' . $totalScore . '">
' . $adTitle . '<br />' . $adIntro . '<br /><br />' . $adContent . '
<br /><br /><br />
Total Score: ' . $totalScore . '<br />
Title Score: ' . $titleScore . '<br />
Intro Score: ' . $introScore . '<br />
Content Score: ' . $contentScore . '<br /><br />
</div>
';
}
}