Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8007745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:55:48+00:00 2026-06-04T17:55:48+00:00

I have put together a mini search algorithm which assigns a score to a

  • 0

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>
    ';

}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:55:49+00:00Added an answer on June 4, 2026 at 5:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have put together a script which is very much like the flickr photostream
I have put together this little script which uses a list of words to
My relatively large program has a PDF help file which I have put together.
I have put together a very complicated search form. Using arrays I have managed
I have put together a script which will upload a CSV file and then
I have put together a for loop for a small search function I am
I have put together a simple ASP.NET MVC 3 test web app that uses
So I have a view that I have put together in another .xib file.
Based on some of my previous questions I have put together the following: var
I am a powershell novice. After days of searching.... I have put together a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.