codevomit
asked on
Print database tree from a selected leaf into a UL list
Hello all,
I have a tree structure in my data base as desicribed:-
I want to be able to from any leaf in the tree recusivly get all it's siblings, then it's parent and all the parent's siblings untill I get back to the root. And then print out in order from Root to selected Leaf in a nested UL list.
Thanks in advance
I have a tree structure in my data base as desicribed:-
-- -----------------------------------------------------
-- Table `filter`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `filter` (
`id` BIGINT(8) UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(255) NOT NULL ,
`parent` BIGINT(8) UNSIGNED NULL DEFAULT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_filter_filter` (`parent` ASC) ,
CONSTRAINT `fk_filter_filter`
FOREIGN KEY (`parent` )
REFERENCES `filter` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I want to be able to from any leaf in the tree recusivly get all it's siblings, then it's parent and all the parent's siblings untill I get back to the root. And then print out in order from Root to selected Leaf in a nested UL list.
Thanks in advance
ASKER
Thanks for getting back to me, it's almost there. This is what I currently have:-
Expected output:-
What I'm actually getting:-
INSERT INTO `filter` (`id`, `name`, `parent`) VALUES
(4, 'C', NULL),
(5, 'D', NULL),
(11, 'G', NULL),
(2, 'A', 11),
(3, 'B', 2),
(6, 'E', 4),
(8, 'F', 5),
(12, 'H', 3),
(14, 'I', 3),
(15, 'J', 14),
(16, 'K', 15),
(17, 'L', 2);
<?php
//error_reporting(E_ALL);
mysql_connect("localhost", "root", "");
mysql_select_db("shop");
function print_list($id, $html) {
$output = null;
$query = "SELECT * FROM filter WHERE parent IN (SELECT parent FROM filter WHERE id " . (($id > 0) ? ("='$id'") : ("IS NULL")) . ") ORDER BY `name` ASC";
$results = mysql_query($query);
if ($id) { {
$output = "<ul>";
$parent = null;
while ($row = mysql_fetch_array($results)) {
$parent = $row['parent'];
$output .= "<li>" . $row['name'] . ($id == $row['id'] ? $html : null) . "</li>";
}
$output .= "</ul>";
$output .= print_list($parent, $output);
}
return $output;
}
}
echo print_list(16, null);
?>
Expected output:-
C
D
G
--A
---- B
------ H
------ I
-------- J
---------- K
---- L
What I'm actually getting:-
K
J
-- K
H
-- I
---- J
------- K
B
-- H
-- I
---- J
----- K
L
A
-- B
---- H
---- I
------ J
-------- K
-- L
I think the issue is that you mis-copied this line:
You want to completely replace $output at this point, not add to it, so:
$output .= print_list($parent, $output);
You want to completely replace $output at this point, not add to it, so:
$output = print_list($parent, $output);
ASKER
Yeah, I did that on purpose because it results in $output being blank.
ASKER
You can see that $output will always result as null without having to execute the code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I will try that out.
Open in new window