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);
?>
Code: Select all
Puslapio pavadinimas
parent of: Puslapio pavadinimasTest page
parent of: Puslapio pavadinimasTest
parent of: TestTest2
parent of: TestTest3
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');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]