Page 1 of 1

Help with this code in changing the display format

Posted: Sat Nov 10, 2007 8:14 pm
by legend986
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I basically have some data like:

A->B
B->C,Y,Z
C->X,D
E->F
G->H

I'm trying to write a recursive function that will display the data in this format:

A->B(C(X,D),Y,Z)
E->F
G->H

Code: Select all

$parent = 72271378;
display_children($parent,0);

function display_children($parent, $level) {
   
   // retrieve all children of $parent
   $sql = "SELECT ip2 FROM br_unique WHERE ip1='".$parent."';";
   //echo $sql;
   $result = mysql_query($sql);    

   // display each child
   while ($row = mysql_fetch_array($result)) {
       // indent and display the title of this child
       echo str_repeat('--',$level).$row['ip2']."\n";

       // call this function again to display this
       // child's children
       display_children($row['ip2'], $level+1);
    }
}
It is displaying data in the following format:

Code: Select all

-902242486
---621217158
-----1065699543
------1095588273
--------209322518
----------1115793954
00-----------964567038
-------------2043159798
---------------855658959
00---------------802026187
----------------1159506805
-------------------1929969091
---------------------1073397243
----------------------1066467717
-------------------------2115174647
00----------------------1109136477
-------------------------2108616823
00-------------------------810809075
00-----------------------808284519
------------------------1211391022
The point where it says 00 is the point where it has to start a new line. It indicates that there are no more children and this is a new parent. i.e. something like the following:

Code: Select all

-902242486
---621217158
-----1065699543
------1095588273
--------209322518
----------1115793954
-964567038
---2043159798
------855658959
-802026187
---1159506805
------1929969091
---------1073397243
------------1066467717
---------------2115174647
-1109136477
---2108616823
-810809075
-808284519
---1211391022
Can someone help me please?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]