Jump to content

[SOLVED] I need help with this recursive function.


takato

Recommended Posts

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.

Link to comment
Share on other sites

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
        )

)

Link to comment
Share on other sites

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..
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.