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

  • Home
  • SEARCH
  • 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 207325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:43:32+00:00 2026-05-11T17:43:32+00:00

I am looking for a cleaner way to do this. My code works, but

  • 0

I am looking for a cleaner way to do this. My code works, but I know it can be better. I have three tables: one with a list of Category Groups, One with a list of categories that are linked to category groups, and one with a list of news stories that are linked to the categories.

I need to loop through all of the names of the Category Groups, followed by the names of the categories that are in the category groups, with the number of news stories in each category listed as well.

I have three tables: CategoryGroups, Categories, News.

I have a set of queries. The first queries all the rows from the CategoryGroups table:

$result = mysql_query( '
SELECT cat_group_id, cat_group_name FROM CategoryGroups 
' );

The second query is inside the looped results of the first query and finds all the categories that have a news item and are linked to a specific category group:

<?php
while( $row = mysql_fetch_assoc( $result ) ){
    $id = $row['cat_group_id'];     
    $name = $row['cat_group_name'];

echo "<h3>$name</h3>";

    $sql =  mysql_query("
            SELECT  category_id, title FROM `Categories`  
            WHERE cat_group_id = $id 
            AND category_id IN 
            (SELECT news.category_id FROM news)
            "); 
    while( $row = mysql_fetch_assoc($sql) ) {
    $title = $row['title'];
    $catid = $row['category_id'];
    $numbers =  mysql_query("
                SELECT * FROM news 
                WHERE category_id =$catid"
                );
    $nums = mysql_num_rows($numbers);
    echo "$title ($nums)<br/>\n";
}
?>

I would like to limit this to one or two queries, with efficiency in mind. I know this can be done, however I have not been successful in my attempts.

thanks.

  • 1 1 Answer
  • 1 View
  • 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-11T17:43:32+00:00Added an answer on May 11, 2026 at 5:43 pm

    I suggest you need to get a book on SQL, such as “SQL Queries for Mere Mortals.”

    $sql = mysql_query("
            SELECT  cg.cat_group_name, c.title, COUNT(n.category_id) AS NumNews
            FROM `CategoryGroups` cg 
            JOIN `Categories` c USING (cat_group_id)
            JOIN `News` n USING (category_id)
            GROUP BY cg.cat_group_name, c.title");
    

    Then loop over the result and output a new <h3> each time the cat_group_name is different from the previous row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 163k
  • Answers 163k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The best way in an eclipse application is to use… May 12, 2026 at 12:10 pm
  • Editorial Team
    Editorial Team added an answer Try using a dynamic programming approach instead: Create an array… May 12, 2026 at 12:10 pm
  • Editorial Team
    Editorial Team added an answer 255 is the default size provided to a byte[]. Specify… May 12, 2026 at 12:10 pm

Related Questions

Is there a way to find the number of files of a specific type
Now that I am able to set the content of my second wizard's page
I have an ASP.NET MVC view which contains checkboxes for user-defined categories. <td><% foreach
The System.Exception.HResult property is protected. How can I peek inside an exception and get

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.