PHP/MySql drawing recursive tree

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

PHP/MySql drawing recursive tree

Post by spamyboy »

Read second reply for example.

I'm trying to play a little with recursive data fetching and here is what I got for now.

Code: Select all

 
<?php
// recursive function
function recursive_read_static ($start_id) {
    global $mysql;
    $row                    = $mysql->query_one_row("SELECT `static_id`, `static_title`, `static_parent_id` FROM `gcms_static` WHERE `static_id`='".$start_id."';");
    $row['parent_title']    = $mysql->query_one_result("SELECT `static_title` FROM `gcms_static` WHERE `static_id`='".$row['static_parent_id']."';");
    
    if (!empty($row['parent_title'])) {
        echo 'parent of: <em>'.$row['parent_title'].'</em>';
    }
    echo '<strong>'.$row['static_title'].'</strong><br />';
    
    $result = $mysql->query_all_rows("SELECT `static_id`, `static_title`, `static_parent_id` FROM `gcms_static` WHERE `static_parent_id`='".$row['static_id']."';");
    if (!empty($result)) {
        foreach ($result as $r) {
            recursive_read_static($r['static_id']);
        }
    }
}
 
recursive_read_static(1);
?>
 
So basically output now is:

Code: Select all

 
Puslapio pavadinimas
parent of: Puslapio pavadinimasTest page
parent of: Puslapio pavadinimasTest
parent of: TestTest2
parent of: TestTest3
 
MySql looks ~~like:

Code: Select all

CREATE TABLE `gcms_static` (
  `static_id` int(10) NOT NULL auto_increment,
  `static_parent_id` int(11) NOT NULL default '0',
  `static_position` int(10) NOT NULL default '0',
  `static_title` varchar(100) collate utf8_unicode_ci NOT NULL default 'unnamed'
  PRIMARY KEY  (`static_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=8 ;
 
INSERT INTO `gcms_static` (`static_id`, `static_parent_id`, `static_position`,`static_title`) VALUES 
(1, 0, 1, 'Puslapio pavadinimas'),
(2, 1, 1, 'Test page'),
(3, 1, 2, 'Test'),
(4, 3, 2, 'Test2'),
(5, 3, 3, 'Test3');
So the result that I'm trying to reach is:

Code: Select all

 
Puslapio pavadinimas
#parent of: Puslapio pavadinimas [b]Test page[/b]
#parent of: Puslapio pavadinimas [b]Test[/b]
##parent of: Test [b]Test2[/b]
##parent of: Test [b]Test3[/b]
 
Any help or alternative suggestions are welcome.
Last edited by spamyboy on Thu Oct 30, 2008 8:31 am, edited 1 time in total.
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Re: PHP/MySql recursive tree-draw help

Post by spamyboy »

I see it will be better just to say what I am trying to create.
I need to make a drop-down list for users to select under which page to add a sub-page.
And I want to display current tree like this:

Code: Select all

Page
? His sub-page
? His second sub-page
  ? Sub page of [i]His second sub-page[/i]
Other no_parent page
And so on.

So I need a little help from people maybe that have done this before.
Post Reply