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.