473,881 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need serious help with a recursive(?) tree function

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="showBr anch('2');">Pag e A 1</div>
<span class="branch" id="2">
<div class="trigger" onClick="showBr anch('3');">Pag e A 1.1</div>
<span class="branch" id="3">
Page A 1.1.1<br>
<div class="trigger" onClick="showBr anch('5');">Pag e A 1.1.2</div>
<span class="branch" id="5">
Page A<br>
Page A 1.2<br>
Page B 1<br>
<div class="trigger" onClick="showBr anch('9');">Pag e C 1</div>
<span class="branch" id="9">
Page C 1.1<br>
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="showBr anch('2');">Pag e A 1</div>
<span class="branch" id="2">
<div class="trigger" onClick="showBr anch('3');">Pag e A 1.1</div>
<span class="branch" id="3">
Page A 1.1.1<br>
<div class="trigger" onClick="showBr anch('5');">Pag e A 1.1.2</div>
<span class="branch" id="5">
Page A<br>
Page A 1.2<br>
(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="showBr anch('9');">Pag e C 1</div>
<span class="branch" id="9">
Page C 1.1<br>
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";

$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
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($PageArra y)) {
if ($PageArray[$poi+1][0] $PageArray[$poi][0]) {
echo "<div class=\"trigger \" onClick=\"showB ranch('" .
$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($PageArra y)-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 );


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!

Jul 7 '06 #1
0 1842

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Derek Rhodes | last post by:
using Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 OK, I have a recursive function that should return a list, but doesn't <start session> def test(word): if type(word) == str:
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays a sequence of spaces; recursive function 2 uses input to display ascending sequence of digits; likewise, recursive function 3 uses input to display descending sequence of digits. I have not followed the instructions completely regarding the...
by: Rodusa | last post by:
I am having problem to apply updates into this function below. I tried using cursor for updates, etc. but no success. Sql server keeps telling me that I cannot execute insert or update from inside a function and it gives me an option that I could write an extended stored procedure, but I don't have a clue of how to do it. To quickly fix the problem the only solution left in my case is to convert this recursive function into one recursive...
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is ListType: for a in arg1: my_function(a, arg2, opt1=opt1, opt2=opt2, opt3=opt3) return if type(arg2) is ListType:
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. However, it is not as straightforward to determine the base address for my stack space. The approach I have taken is to save the address of an automatic variable in main( ), and assume this is a fairly good indicator of my base address. Then, I can...
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole stack and continue execution at the point of the initial call. Is that possible? Thanks, Bill
by: adato | last post by:
hello. I have a problem to build a recursive function The function gets an array of prices, an array of weight and and spesific Price. the output's function is the minimum weighet of the subset and the summry of the subset values is equall or bigger then the specific Price. help me to build the function in a recursive way. (C) thank you
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the object.The object is triangulated. The rays can undergo multiple reflections, diffractions etc of the same object i.e. a ray hits a surface of the object, undergoes reflection resulting in a reflected ray which can again hit a surface, corner or edge...
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any other choice? For example, def func(self, x, y, A, B, C): #x, y change in recursive call
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.