简体   繁体   中英

Recursive tree traversal with PHP - layout issue

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 UI 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!

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.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM