have a poll table which has 10 categories and I want to view the top 5 results from highest to lowest limit into 5 categories. How can I do this in php and mysql?
here are example tables
categories
- id
- category_name
Votes
- id
- category_id
- user_id
Users
- id
- username
here is my sample query:
$q = mysql_query("SELECT * from categories");
while($data = mysql_fetch_array($q){
$votes = mysql_query("SELECT * from votes where category_id=".$data['id']."");
$data_vote = mysql_nuw_rows($votes);
echo $data['category_name']."has".$data_vote."Votes";
}
I want my output to be like this from the highest votes in a categories
category1 has 30 votes
category3 has 25 votes
category5 has 23 votes
category2 has 15 votes
category4 has 10 votes