LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2012, 10:27 AM   #1
countrydj
Member
 
Registered: Jun 2009
Location: Preston, England
Distribution: Centos 6
Posts: 127

Rep: Reputation: 1
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,
 
Old 07-19-2012, 11:19 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
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));
...
 
Old 07-19-2012, 11:51 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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.
 
Old 07-20-2012, 03:53 AM   #4
countrydj
Member
 
Registered: Jun 2009
Location: Preston, England
Distribution: Centos 6
Posts: 127

Original Poster
Rep: Reputation: 1
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...
 
Old 07-20-2012, 04:01 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] MySql order by COUNT and list the total count countrydj Programming 3 07-19-2012 12:21 PM
Top ten list of web browsers order by popularity among Linux users? stf92 General 10 07-01-2011 12:13 PM
List 4 names from users list and output them to fbusers in numbered ascending order? fezzie Programming 4 02-10-2010 01:05 PM
How to list the 'chkconfig --list' in alphabetical order? thomas2004ch Linux - Newbie 3 01-08-2010 02:09 AM
list menu to get results from database alexandros27 Programming 2 05-22-2009 09:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration