• 0

[PHP] asort not ordering correctly


Question

Hey,

So for uni im having to make a website that has a database connection. so i chose something reletively easy, a blog.

That hard part for me was doing a search, more specifcally ordering the results by relevance.

I have devised a process of giving each blog post a score based on its content, and i put all of these score in an array.

I then use asort($array, SORT_NUMERIC) to order the array highest number to lowest.

This worked find untill a friend of my searched something, and it returned results in the wrong order.

A post that got a score of 1 was above a post with a score of 4.

I dont have the ability to show you guys the code atm, so i was hoping some smart person could tell me a better php function to use to sort this array?

Alternatively, should i try and sort the arrray manually? would this give me a more reliable result?

I will be at home later, and will be able to upload the code if needs be.

Any help is appreciated :D

Thanks in advance

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

Hey,

So for uni im having to make a website that has a database connection. so i chose something reletively easy, a blog.

That hard part for me was doing a search, more specifcally ordering the results by relevance.

I have devised a process of giving each blog post a score based on its content, and i put all of these score in an array.

I then use asort($array, SORT_NUMERIC) to order the array highest number to lowest.

This worked find untill a friend of my searched something, and it returned results in the wrong order.

A post that got a score of 1 was above a post with a score of 4.

I dont have the ability to show you guys the code atm, so i was hoping some smart person could tell me a better php function to use to sort this array?

Alternatively, should i try and sort the arrray manually? would this give me a more reliable result?

I will be at home later, and will be able to upload the code if needs be.

Any help is appreciated :D

Thanks in advance

Why not get the database to sort it instead? Like for example:

SELECT fields FROM table WHERE body LIKE '%hi%' ORDER BY score DESC

Hope that helps?

Link to comment
Share on other sites

  • 0

Why not get the database to sort it instead? Like for example:

SELECT fields FROM table WHERE body LIKE '%hi%' ORDER BY score DESC

Hope that helps?

Thanks for the reply.

Unfortunately I cant get the database to sort it, as im doing all the processing that calculates the score after querying the database.

If you know of a way that i can query the database and have the results returned to me in order of relevancy, i will first cry (because of the amount of time i have spent trying to get this method working) and then love you forever :D

and if all else fails, any one else got any ideas?

Thanks in advance :D

Link to comment
Share on other sites

  • 0

Thanks for the reply.

Unfortunately I cant get the database to sort it, as im doing all the processing that calculates the score after querying the database.

If you know of a way that i can query the database and have the results returned to me in order of relevancy, i will first cry (because of the amount of time i have spent trying to get this method working) and then love you forever :D

and if all else fails, any one else got any ideas?

Thanks in advance :D

In that case something like

SELECT * FROM articles WHERE MATCH (title,body) AGAINST('%text%' IN BOOLEAN MODE);

may be more useful? See the mysql docs on that.

Hope that helps. I haven't personally used this exact method, but I've seen it done a few times in tutorials and the like.

Link to comment
Share on other sites

  • 0

In that case something like

SELECT * FROM articles WHERE MATCH (title,body) AGAINST('%text%' IN BOOLEAN MODE);

may be more useful? See the mysql docs on that.

Hope that helps. I haven't personally used this exact method, but I've seen it done a few times in tutorials and the like.

Thanks for the reply again :D

I tried using Fulltext search before, but im no where near as familiar with MYSQL as i am with PHP, so after failing at life trying to get fulltext search working i decided to stick to what i know.

If a solution to the code ive got atm could be found, i would prefer that, otherwise i will do a rewrite and try my hand at fulltext search again.

I'll attach the code so you guys can check it out.

functions.php

results.php

Link to comment
Share on other sites

  • 0

could you post an example of what it's returning and what you're expecting it to return?

Thanks for the reply,

The image shows the results when searching for "php search".

The layout of the results is

Title - Score

Post

You can see that the last post has a score of 4, and the 3rd post has a score of 1.

post-294940-12683519150465_thumb.png

Link to comment
Share on other sites

  • 0

I haven't tested this but go to the results.php file you posted and replace...

array_multisort($scores, SORT_NUMERIC);

With...

function score_sort($a, $b) {
	return ($a['score'] == $b['score']) ? 0 : (($a['score'] > $b['score']) ? -1 : 1);
}

usort($scores, 'score_sort');

Link to comment
Share on other sites

  • 0

I haven't tested this but go to the results.php file you posted and replace...

array_multisort($scores, SORT_NUMERIC);

With...

function score_sort($a, $b) {
	return ($a['score'] == $b['score']) ? 0 : (($a['score'] > $b['score']) ? -1 : 1);
}

usort($scores, 'score_sort');

Wells, that seems to have both solved the problem, but also made the problem worse :D

The results are now the wrong way round (showing lowest score first), how can i make it show highest first with the code you have given me?

Thanks in advance :D your awesome :D

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.