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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:10:52+00:00 2026-05-22T02:10:52+00:00

Please see the data tables and query below .. Items Id, Name 1, Item

  • 0

Please see the data tables and query below ..

Items
Id, Name
1, Item 1
2, Item 2

Categories
Id, Name, Parent ID
1, Furniture , 0
2, Tables, 1
3, Beds, 1
4, Dining Table, 2
5, Bar Table, 2
4, Electronics, 0
5, Home, 4
6, Outdoors, 4
7, Table lamp, 4

ItemCategory
ItemId, CategoryId
1, 2 .. Row1
2, 4 .. Row 2
2, 5 .. Row 3 

ItemCategory table stores which items belongs to which category. An item can belong to top level and or sub category. there are about 3 level deep categories, that is, Tob level, sub level, and sub sub level.

Users select all of the categories they want to view and submit and I can query the database by using a sample query below..

    SELECT * FROM items i INNER JOIN ItemCategory ic ON 
ic.itemId = i.itemId AND ic.itemId IN ('comma separated category ids')

This works fine.

My question is that Is it possible to view all the items under a top level category even though it has not been directly assigned to the item. For example, if users select Furniture above, then it lists all the items belonging to its sub categories (even though the ItemCategory doesn’t contain any record for it)??

I’m open to making necessary amendements to the data table or queries, please suggest a solution. Thank you.

  • 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-22T02:10:53+00:00Added an answer on May 22, 2026 at 2:10 am

    Watcher has given a good answer, but I’d alter my approach somewhat to the following, so you have a structured recursive 2-dimensional array with categories as keys and items as values. This makes it very easy to print back to the user when responding to their search requirements.

    Here is my approach, which I have tested:

    $items = getItemsByCategory($topCategory);
    //To print contents
    print_r($items);
    
    function getItemsByCategory($sid = 0) {
      $list = array();
      $sql = "SELECT Id, Name FROM Categories WHERE ParentId = $sid";
      $rs = mysql_query($sql);
      while ($obj = mysql_fetch_object($rs)) {
        //echo $obj->id .", ".$parent." >> ".$obj->name."<br/>";
        $list[$obj->name] = getItems($obj->id);
        if (hasChildren($obj->id)) {
            array_push($list[$obj->name],getItemsByCategory($obj->id));
        }
      }
      return $list;
    }
    
    function getItems($cid) {
      $list = array();
      $sql = "SELECT i.Id, i.Name FROM Items p INNER JOIN ItemCategory ic ON i.id = ic.ItemId WHERE ic.CategoryId = $cid";
      $rs = mysql_query($sql);
      while ($obj = mysql_fetch_object($rs)) {
        $list[] = array($obj->id, $obj->name);
      }
      return $list;
    }
    
    function hasChildren($pid) {
      $sql = "SELECT * FROM Categories WHERE ParentId = $pid";
      $rs = mysql_query($sql);
      if (mysql_num_rows($rs) > 0) {
        return true;
      } else {
        return false;
      }
    }
    

    Hope this helps.

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

Sidebar

Related Questions

Please Consider the following table : As you can see the Name column has
Please see code below. The destructors are never called. Anyone know why and how
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
everyone. Please see example below. I'd like to supply a string to 'schedule_action' method
I have an Adorner which adornes a Border (please see screenshot below). The MouseDown
I have sql server procedure, please see below. ALTER PROCEDURE [dbo].[uspInsertDelegate] ( @CourseID int,
(For a quick recap please see sections titled , DATA. Can only get results
I have 2 related Linq to SQL questions. Please see the image below to
Please see here for a related question . However, char goes to 0xffff (or
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i

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.