I have put together a mini search algorithm which assigns a score to a search result depending on what they typed and factors like the number of links in the body of the test, spam words etc.
Unfortunately I have no idea how to order the results depending on what this number is generated as. Any ideas on ordering PHP results on a “score” which is assigned to it? I should add that this score isn’t held in the database but is generated each time based on what the user searched for.
OK, so this lists all the results in order dependant on what search term was used, I need it to be ordered by the “TotalScore” but I can’t work out how to do it…
Here is the code:
$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>
';
}
}
Check out the usort function. You provide it with a comparison function of your own which compares two elements of the array. It should be easy to write a callback function comparing the scores of two elements from your array.