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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:11:47+00:00 2026-06-09T23:11:47+00:00

I have this category table Category ============ id name ref_id 1 cat1 0 2

  • 0

I have this category table

Category
============
id    name    ref_id
1     cat1    0
2     cat2    0
3     subCat1 1
4     subCat2 1
5     subCat3 2
6     subCat4 2

I would like to have a result that look like this:

{items: [
    {catId: 1, subCat:[{name: subCat1}, {name:subCat2}]},
    {catId: 2, subCat:[{name: subCat2}, {name:subCat4}]}
]}

I’ve tried to approach this by:

$db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', '', '');

$categories = $db -> query('SELECT * FROM category');

$results = array('items' => array());

foreach($categories as $row) {
    $subCategories = array();
    $results['items'][] = array('id' => $row['video_id'], 'subCat' => $subCategories);
}

echo json_encode($results);

I have no clue of what to do with subCategories in this case. Or Anything I do to fix the query of $categories? Any suggestions? thanks.

  • 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-06-09T23:11:49+00:00Added an answer on June 9, 2026 at 11:11 pm

    Getting the root categories, you need the add where ref_id=0 to your query. And then in your foreach loop, you need to query again to get the current row’s sub-categories (using "where ref_id=".$row['id'] in the second query). You compile the $subCategories array using another foreach loop. O, and I replaced 'id' => $row['video_id'] from your code, to 'catId' => $row['id'] to get the result you require.

    As follows:

    <?php
    $db = new PDO('mysql:host=localhost;dbname=test', '', '');
    
    $categories = $db->query('SELECT * FROM category where ref_id=0');
    
    $results = array('items' => array());
    
    foreach($categories as $row) {
      $subCategories = array();
    
      $sCategories = $db->query('SELECT * FROM category where ref_id='.$row['id']);
      foreach($sCategories as $subCat){
        $subCategories[] = array('name'=>$subCat['name']);
      }
    
      $results['items'][] = array('catId' => $row['id'], 'subCat' => $subCategories);
    }
    
    echo json_encode($results);
    

    I have recreated your database on my side, and when I run this, I get

    {"items":[
       {"catId":"1","subCat":[{"name":"subcat1"},{"name":"subcat2"}]},
       {"catId":"2","subCat":[{"name":"subcat3"},{"name":"subcat4"}]}
    ]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like this: category category_id name category_seo_friendly_url left_id right_id
I have an JPA entity like this: @Entity @Table(name = category) public class Category
I am using Hibernate with spring. I have a model-class like this. @Entity @Table(name
I got a product table like this id | name | category -------------------------- 1
If I have a query like this: String Category = HttpContext.Current.Request.QueryString[Product].ToString(); IQueryable<ItemFile> pressReleases =
I have some codes like this: cats = Category.objects.filter(is_featured=True) for cat in cats: entries
I have something like this: [Description(Sets the color.), Category(Values), DefaultValue(Color.White), Browsable(true)] public Color MyColor
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have the following MySql Table: -------------------------------------------- Table 'category' -------------------------------------------- category_id name parent_id 1
i have MySql Table name 'category', in that table i have id catname parent_id

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.