recursion function for xml


/ Published in: PHP
Save to your folder(s)

your database needs id, bezeichner and parent fields


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. mysql_connect("localhost","root","root") or die (mysql_error());
  4.  
  5. function xmlloop($intParent=0, $intEbene=0){
  6. $space = null;
  7.  
  8. $sql = mysql_query("SELECT * FROM xmltest WHERE parent = ".$intParent);
  9.  
  10. while($query = mysql_fetch_assoc($sql)){
  11.  
  12. for ($i=0;$i<$intEbene;$i++) $space .= ' ';
  13.  
  14. echo $space.'<'.$query["bezeichnung"].' de="'.$query["val"].'">'."\n";
  15.  
  16. $child = mysql_fetch_assoc(mysql_query("SELECT COUNT(id) as anzahl FROM xmltest WHERE parent = ".$query["id"]." GROUP BY id"));
  17.  
  18. if($child["anzahl"]>0){
  19. xmlloop($query["id"], ($intEbene+1));
  20. }
  21.  
  22. echo $space.'</'.$query["bezeichnung"].'>'."\n";
  23.  
  24. }
  25. }
  26.  
  27. xmlloop();
  28. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.