By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,117 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,117 IT Pros & Developers. It's quick & easy.

Need serious help with a recursive(?) tree function

P: n/a
Hi Guys(I apologize for the lengty post - Im trying to explain it as
best i can)

I've been cracking my head on this one for the past 24+ hours and i
have tried creating the function in ten different ways and none of the
versions i've made works exactly as it should.

I have an array called $PageArray which contains a sorted list of all
pages in my application. Im trying to create a recursive function(It
dosn't need to be recursive if it can be done in another way) which
loops thorugh the $PageArray and writes some DIV and SPAN tags arround
certain parts so i can create a javascript tree from it - The problem
is with the closing SPAN tags which all dosn't get written to the
browser and i just can't figure out what stupid mistake im making so
please help me.

Here is an example of what i want it to write with the code i will
provide you with:
<div class="trigger" onClick="showBranch('2');">Page A 1</div>
<span class="branch" id="2">
<div class="trigger" onClick="showBranch('3');">Page A 1.1</div>
<span class="branch" id="3">
Page A 1.1.1<br>
<div class="trigger" onClick="showBranch('5');">Page A 1.1.2</div>
<span class="branch" id="5">
Page A 1.1.2.1<br>
</span>
Page A 1.2<br>
</span>
</span>
Page B 1<br>
<div class="trigger" onClick="showBranch('9');">Page C 1</div>
<span class="branch" id="9">
Page C 1.1<br>
</span>
Page D 1<br>
But what it really writes at the moment(The different versions i've
made have produced different outputs but all where lacking some of the
code needed):
<div class="trigger" onClick="showBranch('2');">Page A 1</div>
<span class="branch" id="2">
<div class="trigger" onClick="showBranch('3');">Page A 1.1</div>
<span class="branch" id="3">
Page A 1.1.1<br>
<div class="trigger" onClick="showBranch('5');">Page A 1.1.2</div>
<span class="branch" id="5">
Page A 1.1.2.1<br>
</span>
Page A 1.2<br>
</span>
(Here im missing another closing SPAN tag as the one above - Thats the
only thing which makes this not work - When the HTML is written to the
browser it actually has an empty line where the last closing span
should be(Right here where im writing this note)?)
Page B 1<br>
<div class="trigger" onClick="showBranch('9');">Page C 1</div>
<span class="branch" id="9">
Page C 1.1<br>
</span>
Page D 1<br>
This output is actually fully working except one thing is missing - A
single SPAN tag. This output is with a 4 level deep structure - If i
only have 3 levels this code actually works which is what puzzles me. I
know im overlooking something completely stupid.

Here is the entire code which i'm using to test this out(It's a bit
ugly in the way it writes the pagetitle in every if sentence etc.):

<?
// Array for testing
$PageArray = array();
$PageArray[0][0] = 1; // Level
$PageArray[0][1] = 1; // PageID
$PageArray[0][2] = "Page A 1"; // Title

$PageArray[1][0] = 2;
$PageArray[1][1] = 2;
$PageArray[1][2] = "Page A 1.1";

$PageArray[2][0] = 3;
$PageArray[2][1] = 3;
$PageArray[2][2] = "Page A 1.1.1";

$PageArray[3][0] = 3;
$PageArray[3][1] = 4;
$PageArray[3][2] = "Page A 1.1.2";

$PageArray[4][0] = 4;
$PageArray[4][1] = 5;
$PageArray[4][2] = "Page A 1.1.2.1";

$PageArray[5][0] = 2;
$PageArray[5][1] = 6;
$PageArray[5][2] = "Page A 1.2";

$PageArray[6][0] = 1;
$PageArray[6][1] = 7;
$PageArray[6][2] = "Page B 1";

$PageArray[7][0] = 1;
$PageArray[7][1] = 8;
$PageArray[7][2] = "Page C 1";

$PageArray[8][0] = 2;
$PageArray[8][1] = 9;
$PageArray[8][2] = "Page C 1.1";

$PageArray[9][0] = 1;
$PageArray[9][1] = 10;
$PageArray[9][2] = "Page D 1";

/*
Page A 1
Page A 1.1
Page A 1.1.1
Page A 1.1.2
Page A 1.1.2.1
Page A 1.2
Page B 1
Page C 1
Page C 1.1
Page D 1
*/

function sitetree($poi) {
global $PageArray;

if ($poi < count($PageArray)) {
if ($PageArray[$poi+1][0] $PageArray[$poi][0]) {
echo "<div class=\"trigger\" onClick=\"showBranch('" .
$PageArray[$poi+1][1] . "');\">" . $PageArray[$poi][2] . "</div>\n";
echo "<span class=\"branch\" id=\"" . $PageArray[$poi+1][1]
.. "\">\n";
} elseif ($PageArray[$poi+1][0] == $PageArray[$poi][0]) {
echo $PageArray[$poi][2] . "<br>\n";
} elseif ($poi < count($PageArray)-1 and $PageArray[$poi+1][0]
< $PageArray[$poi][0]) {
echo $PageArray[$poi][2] . "<br>\n";
echo "</span>\n";
} else {
echo $PageArray[$poi][2] . "<br>\n";
}
sitetree($poi+1);
$poi++;
}
}

sitetree(0);
?>

In the real code the PageArray get's created from a database and
another recursive function which sorts it in the correct order - But
this is just for testing the function. Of course the function needs to
work no matter how many levels there are.
If i run the function without the level 4 subpage then it works fine so
im overlooking something somewhere.

I REALLY hope you guys can help because im about to give up on this!
Thanks.

Jul 7 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.