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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:05:07+00:00 2026-06-15T20:05:07+00:00

I am creating a questionnaire for a client that requires the questions to be

  • 0

I am creating a questionnaire for a client that requires the questions to be organized by 3 layers of levels. I’ve successfully created the U.I. however I’ve been trying for the last 3 hours to pull data from a database in such a way that everything loads in the right place. The database is organized like so by the client so i have no control over it:

id    description    parentId    about
1      Level 1        0           This is the top level or in my case tab1
2      Level 2        0           This is the next tab in the top level
3      Level 1a       1           This is the first category under tab1
4      Level 1b       1           This is the next category under tab1
5      Level 1a1      3           This is the content under the first category of tab1

So anything with a parentId of 0 is the top level and will contain anything of the second level with the parentId of 1 and so on. Confusing yes, I can barely make sense of this but this is how I’ve been told to do it.

What approach would be the best way to execute something like this? An example from another question I’m using as a reference is attached below (although not working)

foreach (mysql_query("SELECT * FROM pB_test ORDER BY id ASC") as $row) {
  $menuitem = array_merge(array(), $row);
  $menuLookup[$menuitem['id']] = $menuitem;
  if ($menuitem['parent'] == null) {
    $menuitem['path'] = "/" . $menuitem['name'];
    $menu[] = $menuitem[];
  } else {
    $parent = $menuLookup[$menuitem['parent']];
    $menuitem['path'] = $parent['path'] . "/" . $menuitem['name'];
    $parent['menu'][] = $menuitem;
  }
}

Any help would be greatly appreciated. Cheers

  • 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-15T20:05:09+00:00Added an answer on June 15, 2026 at 8:05 pm

    It might be worth doing this in PHP, as opposed to SQL if you’re working with an external database. I haven’t benchmarked the following, so try with your data and see if performance is problematic or not.

    You can choose yourself what to do with orphaned records (which reference parentIDs that don’t exist anymore).

    Ordering in PHP like this requires that you have all of your data beforehand, so use something like PDO’s fetchAll(PDO::FETCH_ASSOC) method, which should result in something like this:

    $data_from_database = array(
        array("id" => 1, "parentId" => 0, "description" => "Level 1"),
        array("id" => 2, "parentId" => 1, "description" => "Level 1a"),
        array("id" => 3, "parentId" => 1, "description" => "Level 1b"),
        array("id" => 4, "parentId" => 0, "description" => "Level 2"),
        array("id" => 5, "parentId" => 2, "description" => "Level 1a1"),
        array("id" => 6, "parentId" => 5, "description" => "Level 1a11a"),
        array("id" => 7, "parentId" => 5, "description" => "Level 1a11b"),
        array("id" => 8, "parentId" => 9, "description" => "Level 3"),
    );
    

    First off, you’ll want to have the primary key (ID) as the array’s keys. The following also adds the keys “children” and “is_orphan” to every record.

    $data_by_id = array();
    foreach($data_from_database as $row)
        $data_by_id[$row["id"]] = $row + array(
            "children" => array(), 
            "is_orphan" => false
        );
    

    This will look something like this:

    $data_from_database = array(
        1 => array("id" => 1, "parentId" => 0, "description" => "Level 1", 
                   "children" => array(), "is_orphan" => false),
        ...
     );
    

    Now, it gets tricky: we’ll loop through the array and add references.

    foreach($data_by_id as &$row)
    {
        if($row["parentId"] > 0)
        {
            if(isset($data_by_id[$row["parentId"]]))
                $data_by_id[$row["parentId"]]["children"][] = &$row;
            else
                $row["is_orphan"] = true;
        }
    }
    unset($row); // Clear reference (important).
    

    The last step is to clean up the ‘root’ of the array. It’ll contain references to duplicate rows.

    foreach($data_by_id as $id => $row)
    {
        // If you use this option, you'll remove
        // orphaned records.
        #if($row["parentId"] > 0)
        #    unset($data_by_id[$id]);
    
        // Use this to keep orphans:
        if($row["parentId"] > 0 AND !$row["is_orphan"])
            unset($data_by_id[$id]);
    }
    

    Use print_r($data_by_id) after every step to see what happens.

    If this proves to be a time consuming operation, try to build up the tree by only doing SELECT id, parentId FROM ... and then later fetching the metadata such as description. You could also store the result in Memcache or serialized into a database.

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

Sidebar

Related Questions

I am creating a questionnaire for a client that requires the questions to be
I'm creating a framework for questionnaires. The questionnaire has several questions. My situation is
OK, I am creating a questionnaire application that is managed by a single user
I am currently working on a project that involves creating a questionnaire from a
Creating a voting system just like stackoverflow. Questions like this have been asking several
Creating a DIV that uses CSS to draw a triangle to the left. Trying
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a server-side socket will fail if I'm trying to use the same port
Creating a simple RPG game, first time using XNA. Trying to get my character
I am creating an xslt and xml file dynamically to show my questionnaire &

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.