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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:40:15+00:00 2026-05-25T19:40:15+00:00

I’m having trouble getting started with the following: I have a database of questions

  • 0

I’m having trouble getting started with the following:

  • I have a database of questions and multiple answers per question.
  • Each answer belongs to one question.
  • Each answer points to one (different) question.

I’m trying to create a multi-level tree visualization of the entire relationship path without knowing the number of levels. How would I best do this?

Here is an example of the data array (simplififed):

array(
   1 => array( //this key is the answer id
      question_id = 1,
      answers = array(
         0 => answer_opens_question__id = 2,
         1 => answer_opens_question__id = 3,
         2 => answer_opens_question__id = 5
      )
   ),

   2 => array( 
      question_id = 2,
      answers = array(
         0 => answer_opens_question__id = 1,
         1 => answer_opens_question__id = 5
      )
   ),

   5 => array( 
      question_id = 3,
      answers = array(
         0 => answer_opens_question__id = 2
      )
   )
)

In theory, this could go on forever and end up in an infinite loop – it’s highly unlikely that this will ever happen though, since this data is user generated and not dynamically created.

I’m only looking for a way to recursively display a path like question > answer>>question > answer>>question > answer>>question> ... – the output will be in HTML with nested ul-li.

Any ideas on how I could do this? Thanks a lot for input of any kind.


EDIT/Solution:

Thanks to DaveRandom, I got it: write a function that loops through each answer and calls itself for each.

function recursiveTree($id) {               
                global $data; //an array like the one above
                if(array_key_exists($id, $data)) {
                    echo '<ul><li>';                        
                    echo $data[$id]['question_name']; //question

                    if(is_array($data[$id]['answers']) && !empty($data[$id]['answers'])) {

                        foreach($data[$id]['answers'] as $answer) {
                            echo '<ul><li class="answer"><div>'.$answer['text'].' opens </div>';
                            recursiveTree($answer['answer_opens_question__id']); //call this very function for this answer's question - that's the trick
                            echo '</li></ul>';
                        }
                    }                           
                    echo '<li></ul>';
                }                   
            }//recursiveTree()

recursiveTree(1); //where 1 is the first question's id

I’m not keeping track of questions yet like Dave is in this code (please check out the accepted answer below) – that will be added. It works like this already though (because I don’t have any loops in my test data), but I agree with everybody that it’s advisable to take care of possible infinite loops.

With some padding and some borders around li.answer, the above output is even readable 🙂

Thanks again DaveRandom.

  • 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-25T19:40:15+00:00Added an answer on May 25, 2026 at 7:40 pm

    Maybe this will give you a prod in the right direction? (Slightly fixed)

    function recursive_tree ($startQuestionId, $alreadyDisplayed = array()) {
      // Make sure we only display each question once
      $alreadyDisplayed[] = $startQuestionId;
      // Replace this with a sensible query to get the answers for question id $startQuestionId
      $answers = $db->query("SELECT answers.answerId, answers.opensQuestionId FROM questions, answers WHERE questions.questionId = '$startQuestionId' AND answers.questionId = questions.questionId");
      // Echo a header for the question
      echo "<div id='question$startQuestionId'>Question $startQuestionId\n<ul>\n";
      while ($row = $db->fetch()) {
        // Loop through the answers to this question
        echo "<li>\nAnswer id {$row['answerId']} opens question id {$row['opensQuestionId']}\n";
        if (!in_array($row['opensQuestionId'],$alreadyDisplayed)) {
          // The linked question hasn't been displayed, show it here
          $alreadyDisplayed = array_merge($alreadyDisplayed,recursive_tree($row['opensQuestionId'],$alreadyDisplayed));
        } else {
          // The linked question has already been displayed
          echo "(<a href='#question{$row['opensQuestionId']}'>Already displayed</a>)\n";
        }
        echo "</li>\n";
      }
      // Close everything off
      echo "</ul>\n</div>\n";
      return $alreadyDisplayed;
    }
    
    // And call it like this
    $questionToStartAt = 1;
    recursive_tree($questionToStartAt);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a reasonable size flat file database of text documents mostly saved in
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and

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.