takato Posted February 28, 2009 Share Posted February 28, 2009 hi I'm trying to make a function to be able to make a xml style tree with data from mysql. I've been trying for a couple days now but so far I haven't been able to get it to work right. it's only doing part of it, I think it is some problem with my foreach loop it's not looping or something. the mysql table has a ID field called "id" and a Parent ID field called "pid" and a Title field called "title" plus other fields but only need these. ok the first id has a pid of 0 and all the others have a pid of the id it was posted on. here's my php file: tree.php <?php $link = mysql_connect("host","user","pass"); $db_selected = mysql_select_db("database", $link); $query = 'SELECT title, id, pid FROM fsrper ORDER BY id ASC'; $result = mysql_query($query); while ($r = mysql_fetch_assoc($result)) {//open while 1 $id = $r['id']; $pid = $r['pid']; $c["$pid"]["$id"] = $r['title']; }//end while 1 function tree($id='0') {//open tree function global $c; foreach ($c["$id"] as $k => &$v ) { //Open foreach 1 $out2 = '<node lable="'.$v.'" pid="'.$id.'" id="'.$k.'"'; //unset($c["$id"]["$k"]); if (!isset($c["$k"])) { //open if else $out2 .= " isBranch=\"true\" />\n"; } else {// if else halfway $out2 .= ">\n"; $out2 .= tree("$k"); $out2 .= "</node>\n"; }// end if else }//end foreach 1 return $out2; }// end tree function $out = "<?xml version=\"1.0\"?>\n"; $out .= "<node>\n"; $out .= tree(); $out .= "</node>\n"; mysql_close($link); echo "<pre>".htmlentities($out, ENT_QUOTES)."</pre>"; // test it ?> Please help I'm not sure how to get it working. Quote Link to comment https://forums.phpfreaks.com/topic/147259-solved-i-need-help-with-this-recursive-function/ Share on other sites More sharing options...
sasa Posted February 28, 2009 Share Posted February 28, 2009 are you shure that your root elements have pid=0? try to print_r($c); Quote Link to comment https://forums.phpfreaks.com/topic/147259-solved-i-need-help-with-this-recursive-function/#findComment-773086 Share on other sites More sharing options...
takato Posted February 28, 2009 Author Share Posted February 28, 2009 yeah the first one does have a pid of 0 heres the output I get at my test site and the $c array <?xml version="1.0"?> <node> <node lable="one" pid="0" id="1"> <node lable="three" pid="1" id="3"> <node lable="three-five" pid="3" id="5" isBranch="true" /> </node> </node> </node> Array ( [0] => Array ( [1] => one ) [1] => Array ( [2] => two [3] => three ) [2] => Array ( [4] => two-four ) [3] => Array ( [5] => three-five ) ) Quote Link to comment https://forums.phpfreaks.com/topic/147259-solved-i-need-help-with-this-recursive-function/#findComment-773104 Share on other sites More sharing options...
takato Posted February 28, 2009 Author Share Posted February 28, 2009 I figured it out, I needed to put a .= instead of = on the first one too. it kept saving over it self each time it looped and it made look like it wasn't looping correctly. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/147259-solved-i-need-help-with-this-recursive-function/#findComment-773134 Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 also bro, not to contradict you.. but globals are very much unneeded.. there is a much better way with referencing the variable in the function call $omg = "lmao"; whatever($omg); function whatever(&$geeze) { // $geeze will be a variable accessing $omg, instead of just handling teh VALUE of $omg.. meaning any change you do to $geeze in here, affects $omg.. } Quote Link to comment https://forums.phpfreaks.com/topic/147259-solved-i-need-help-with-this-recursive-function/#findComment-773141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.