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 3947204
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:13:26+00:00 2026-05-20T01:13:26+00:00

i have 2 tables: Categories,Oglasi categories category_id | category | parent 1 auto 0

  • 0

i have 2 tables:
Categories,Oglasi

categories
category_id | category | parent
1             auto      0
2             games     0
3             bmw       1
4             cards     2

oglasi
oglas_id | category_id
1          3            
2          4             

What im trying to do is make a menu of this as:

parent name – category name ( number of items in this category)

Example : auto – bmw ( 1 )

Now i got it to make a tree menu of parent name-category name,but don’t know how to connect all of this with the counted rows number. Here’s my query and code to do this:

$kategorije = dbQuerySelect('SELECT a.category parent
                                  , b.category child
                                  FROM categories a
                                  JOIN categories b
                                  ON a.category_id = b.parent
                                  ORDER BY a.category_id');
                if ($kategorije)
                  {
                    $parent = '';
                    echo "<ul>";
                    foreach ($kategorije as $next) {
                       if ($next['parent'] != $parent) {
                          if (strlen($parent) > 0) {
                             echo "    </ul>";
                             echo "  </li>";
                          }
                          echo "  <li>" . $next['parent'];
                          echo "    <ul>";
                       }
                       echo "    <li>" . $next['child'] . "</li>";

                       $parent = $next['parent'];
                    }
                    echo "    </ul>";
                    echo "  </li>";
                    echo "</ul>";
                  }

So in this case this returns:

parent | child
auto     bmw
games    cards

Can someone please help me to edit my code and return the number of rows in each ‘subcategory’ ? I tried a few variations with COUNT in the query,but i just couldn’t make it work.

  • 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-20T01:13:26+00:00Added an answer on May 20, 2026 at 1:13 am
    SELECT a.category parent, 
           b.category child,
           (select count(*) from oglasi where category_id = b.category_id) as count
    FROM categories a
    JOIN categories b
    ON a.category_id = b.parent
    ORDER BY a.category_id
    

    With this you should get:

       parent|child|count
       auto  bmw    1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my scenario: I have two MySQL tables: Categories (columns: id, category) Items
I have 3 tables in my DB (MySQL). categories (name:string) items (name:string, category_id:int) votes
I have three tables. Product, product categories and product category links. The links table
I have 3 tables products categories products_categories ------------ ------------- -------------------- product_id category_id product_id product_title
I have Two tables like this: Table categories: columns: id, name, parent 1, Foods,
I have two tables: categories => Category_ID , Title , Description , Default_Points ,
I have two tables called categories and topics. categories has columns category_id and category_title.
I have these 2 tables categories with fields id, category, total users with fields
I have two tables, one with categories and subcategories. Each category and subcategory has
I have two tables: parent-child 'categories': id name parent_id 1 Food NULL 2 Pizza

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.