I have a function that is supposed to search the db for the highest ‘score’.
The Db is structured like this:
----------------------------------------
| id | UrlId | Article | Score |
----------------------------------------
I can get the highest score correctly, but I do not know how to return the full object based on the highest score.
I am reluctant to loop through the entire table and test the values of ‘score’ to see which is the highest (although as I type that I suspect I am doing it anyway) because the db will potentially have 10000’s of records.
I am sure that this is dead simple, but I have “the dumb and I cant brain today” Does anyone know a more elegant solution?
My end result would have to be something like this:
if there are 4 UrlId;s with the same top score, the user would need to see:
UrlId example1 20(score)
UrlId example2 20(score)
UrlId example3 20(score)
UrlId example4 20(score)
all other results would not be displayed.
function gethappiestBlog() {
$happiestBlogs = /* This is the data that I loop through, this is correct */
$happinessArray = array();
foreach($happiestBlogs as $happiestBlog) {
$happinessArray[]= $happiestBlog->Score;
}
$maxHappy = max($happinessArray);
echo $maxHappy;
}
1 Answer