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 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

Related Questions

Just looking for the best practise way of doing this. I have a table
I am looking for a more jquery/cleaner way to be able to select all
I'm looking for a way to do the following in Ruby in a cleaner
I'd like to use model binding to keep my controllers looking cleaner, you can
I am looking for a way to build a dropup menu. Basically, I have
I'm looking to make a recursive method iterative. I have a list of Objects
I am looking for a way to do this in C#: an Asker object
I know that recursion is sometimes a lot cleaner than looping, and I'm not
Edit: just to be clearer on what I am looking to do. I have
Looking for the best approach: I have a tabbar and a navigationbar. under the

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.