LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I want to list database results in order of popularity, with count (https://www.linuxquestions.org/questions/programming-9/i-want-to-list-database-results-in-order-of-popularity-with-count-4175417616/)

countrydj 07-19-2012 10:27 AM

I want to list database results in order of popularity, with count
 
I am using this code to count and sort from my database:

PHP Code:

$qry "SELECT entertainer, count(*) FROM final_results GROUP by entertainer ORDER by count(*) DESC LIMIT 5"

This is the code that I use to display the results:
PHP Code:

if(!($results mysql_query($qry$link))){
    
displayErrMsg(sprintf("Error in executing %s query"$qry));
    
displayErrMsg(sprintf("error:%d %s"mysql_errno($link), mysql_error($link)));
    exit();
}

$numresults mysql_num_rows($results);
if(
$numresults 0){

    while(
$row mysql_fetch_object($results)){
$cat $row->entertainer;

if (
$cat != "") {
        
$main_content .= '<div align=left><font size=2><b>'.$cat.'<b></font></div>';
    }
else {
        
$main_content .= '<font size=2><b>No votes</b></font>';
    } 

I get the right result, in as much as it lists all the contents in order of popularity, i.e.
Quote:

Entertainer of the year award UK:

Henry Smith
Gary Perkins
John Permentor
Darren Busby
Richard Palmer
What I would like is the top 5 results to display, with a count for each of them and then a total count of all the results.

e.g.

Quote:

Entertainer of the year award UK:

Henry Smith (110)
Gary Perkins (83)
John Permentor (66)
Darren Busby (21)
Richard Palmer (15)

Total votes = 366

I would appreciate any help and advice.

Thanks,

NevemTeve 07-19-2012 11:19 PM

For a start use an alias:

Code:

$qry = "SELECT entertainer, count(*) AS cnt".
" FROM final_results GROUP by entertainer".
" ORDER cnt DESC LIMIT 5";

...
printf ("%s(%d)", htmlspecialchars ($row->entertainer), $row->cnt));
...


grail 07-19-2012 11:51 PM

Is this not a duplicate of your original thread: http://www.linuxquestions.org/questi...nt-4175417597/

If it is, please only ask a question once and stick with it so people can follow the information. If you have new information then add it to your original query.

countrydj 07-20-2012 03:53 AM

Hi grail..
Sorry for the mix up.
I was told, in another forum, that my post should have been in the PHP forum, not MySql.
I started a new thread, thinking that it was going to be in a different forum. I then realised that this wasn't the case.
However, I changed the title so that it featured PHP.
I now believe that it doesn't matter.

SORRY...

grail 07-20-2012 04:01 AM

The one I mentioned is in this forum so please close one and advise to follow the other.


All times are GMT -5. The time now is 01:31 AM.