0

I am creating a questionnaire for a client that requires the questions to be organized by 3 layers of levels, with a unknown amount of questions and categories. I've successfully created the U.I. and the code to pull the information from the database however I've been trying to find out how to get everything loading in the right place. The database is organized like so by the client so I have no control over it:

id description parentId 
1 Level 1 0 
2 Level 2 0 
3 Level 1a 1 
4 Level 1b 1 
5 Level 1a1 3 

and the code I'm using right now is this:

function printChildQuestions($parentid) {
 $sql="SELECT * FROM pB_test WHERE parentID=$parentid";
 $result=mysql_query($sql);
 $i=0;
 while (true) {
 $row=mysql_fetch_array($result);
 if (!$row) break;
 if ($row['parentId'] == 0) {
 echo '<div class="tabs">';
 echo '<span class="level1">'.$row['description'].'</span>';
 echo '<ul id="main-nav"><h2 align="center">Categories</h2></ul>';
 echo '<div>'.$row['id'].'&nbsp;'.$row['description'].'&nbsp;'.$row['parentId'].'</div>';
 if ($row['parentId'] == 1) {
 }
 echo '</div>';
 } else {
 echo '<div>'.$row['id'].'&nbsp;'.$row['description'].'&nbsp;'.$row['parentId'].'</div>';
 }
 printChildQuestions($row['id']);
 }
}
printChildQuestions(0);

and the html that needs to be generated for is here: http://jsfiddle.net/Cyb2N/

The issue is every idea I've come up with needs me to hardcode the levels in and breaks when level 2 is introduced. Could someone shove me in the right direction? Thanks!

asked Jun 13, 2012 at 1:55

2 Answers 2

0

If I understand your question correctly, you're looking for a way to resolve the levels implied by the parentId references in the client's data? Like so:

Level 1 
 Level 1a 
 Level 1a1 
 Level 1b 
Level 2 

How about creating a Level class with a parent property which references the parent Level? Then you can iterate through the client's data, resolving the Level instances into a nice tree, and do anything you want with it from there.

Alternatively, you could give the Level class a children property, which would be a collection of the levels that reference that instance as a parent... anyway the point is that you can transform your client's flat data into a useful tree model with a fairly simple class. By counting parents (or children), you can tell how far up/down the tree a given Level is.

answered Jun 13, 2012 at 2:18

3 Comments

Could you give me an example code if possible? I'll try my side to redo the one I have right now but I have little experience with this type of logic.
Oh wait, you seem to have several overlapping questions out on this topic already. What I'm suggesting is an alternative to the answers you've already gotten to this question. In this question you seem to be attacking the HTML formatting part of your project, in which case my answer is less relevant.
Yeah, I decided to open a new question once I changed my approach. Thanks for the help though!
0

printChildQuestions is a recursive function. It sounds like you don't know how to determine which level of recursion you're at. Here's how:

printChildQuestions($parent_id);
function printChildQuestions($parent_id, $level = 1){
 if( $need_to_recurse ){
 printChildQuestions($new_parent, $level + 1);
 }
}
answered Jun 13, 2012 at 2:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.