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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:17:38+00:00 2026-05-22T21:17:38+00:00

I imagine this is a fairly common requirement for people when it comes to

  • 0

I imagine this is a fairly common requirement for people when it comes to building any kind of application that relies on sorting and displaying data in categories – any CMS/Forums/Carts etc and I’ve been tearing my hair out trying to think of a way to show all categories and their children to no avail – the best I’ve managed is while loop within while loop (however many levels I think I may require) but this defeats the point of programming in my opinion, it needs to be effortlessly extensible.

So, given:

Category 1
-Sub cat
-Sub cat
--Sub sub cat
--- Sub sub cat
-- Sub sub cat
-Sub cat
Category 2
-Sub cat
-Sub cat
--Sub sub cat
---sub sub sub cat
----sub sub sub sub cat
-sub cat
Category 3
-Sub cat

Database fields: ID Name ParentIDS

How would you go about echoing each of the categories in their hierarchy from a database?

I’d post my code, but obviously its massive, given the long-hand way of nesting loops.

I’ve thought about writing bits of code that find the ‘depth’ of the category tree but that still isn’t cutting it.. ideas?

  • 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-22T21:17:39+00:00Added an answer on May 22, 2026 at 9:17 pm

    The structure you’re talking about is essentially a tree. Iterating over a tree is relatively simple if you use recursion. I don’t know what database access code you’re using, but you should be able to extrapolate accordingly from the following psuedocode:

    function iterate_tree(arr) {
        foreach (item in arr) {
            print item;
            iterate_tree(item.children);
        }
    }
    

    In english, that means print an item in the list, then print its children, and once all children (and their children) have printed, move on to the next item in the list.

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

Sidebar

Related Questions

Imagine this scenario: in our company there is an employee that play around graphic,css,html
I'm looking to write what I would imagine is a fairly common macro. I
I tried Googling and couldn't find this, but I imagine it is a fairly
I imagine this is fairly simple and one I should be able to get
I imagine that this is a simple question, but I'm getting some strange results
imagine this html on a page <div id=hpl_content_wrap> <p class=foobar>this is one word and
Imagine this subroutine: sub test(&&) { my $cr1 = shift; my $cr2 = shift;
Imagine this simple form <form action=<?php echo $_SERVER['REQUEST_URI']; ?> method=post> <fieldset> <legend>Contact Me</legend> <label
Imagine this scenario: You have a desktop and a laptop. The desktop has a
Imagine this as the code from build.xml: <project name=test project> <target name=first> <echo>first</echo> </target>

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.