I have query like this:
$sql = "SELECT *
FROM global_info
WHERE title LIKE '%$string%'
OR tag LIKE '%$string%'
OR body LIKE '%$string%'";
How can I count number of results for each row and then order results based on count?
For example
Row 1:
Title:
Nice photo of Adricatic sea
Body:
This is me and my friend at the Adriatic sea, somewhere in Montenegro.
Color of Adricatic sea is amazing.
Tag:
Adriatic sea, photo
Row 2:
Title:
Adricatic sea
Body:
Take at noon
Tag:
Adriatic sea, photo
And someone search:
adriatic sea photo
Row 1 have more results than row 2, and it will be on the first place in the query result.
There are two approaches you can use.
One is to use full text searches – which will take you in a different but very helpful direction. Read about it at http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html.
The other approach is to infer it using strength length changes when you do a text substitution:
Of course, you’d substitute “‘Adricatic'” with the variable holding the word searched for by your user. And you would repeat for each field you want to contribute to the score. Sum the scores and you’ve got your results.