Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1049513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:37:42+00:00 2026-05-16T16:37:42+00:00

have a poll table which has 10 categories and I want to view the

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:37:42+00:00Added an answer on May 16, 2026 at 4:37 pm

    Use:

    $q = mysql_query("SELECT c.category_name,
                             COALESCE(COUNT(v.category_id), 0) AS cnt
                        FROM CATEGORIES c
                   LEFT JOIN VOTES v ON v.category_id = c.id 
                    GROUP BY c.category_name
                    ORDER BY cnt DESC
                       LIMIT 5 ");
    while($data = mysql_fetch_array($q) {
      echo $data['category_name'] ." has ". $data['cnt'] ." votes"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON object items which has few items that I want to
I have a table displaying results from a database, and I am adding some
I have a page which list, a list of volunteers from the volunteer_list table.
I have these tables: Idea and poll (and others) which are types of what
Say I have a database called Poll and have these two table structures. poll
I have something like this in my code: worker.setObject(queue.poll()); I want a queue that
I have a JavaScript code which links to survey poll page. When I am
Trying to setup a simple Thread/Poll table mapping. Here is what I have: Threads
I am creating a poll in php. I have a table with an id
I have a mysql table in which I store jobs to be processed. mainly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.