DB structure is this:
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 1',1,16);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 2',2,3);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 3',4,7);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 4',5,6);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 5',8,13);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 6',9,12);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 7',10,11);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 8',14,15);
When I run this query:
SELECT n.title, COUNT(*)-1 AS depth FROM categories AS n, categories AS p WHERE n.lft BETWEEN p.lft AND p.rgt GROUP BY n.lft ORDER BY n.lft;
$result = mysql_query($query);
// Build array
$tree = array();
while ($row = mysql_fetch_assoc($result)) {
$tree[] = $row;
}
After that I can use following function:
// bootstrap loop
$result = '';
$currDepth = -1; // -1 to get the outer <ul>
while (!empty($tree)) {
$currNode = array_shift($tree);
// Level down?
if ($currNode['depth'] > $currDepth) {
// Yes, open <ul>
$result .= '<ul>';
}
// Level up?
if ($currNode['depth'] < $currDepth) {
// Yes, close n open <ul>
$result .= str_repeat('</ul>', $currDepth - $currNode['depth']);
}
// Always add node
$result .= '<li>' . $currNode['title'] . '</li>';
// Adjust current depth
$currDepth = $currNode['depth'];
// Are we finished?
if (empty($tree)) {
// Yes, close n open <ul>
$result .= str_repeat('</ul>', $currDepth + 1);
}
}
print $result;
Can anyone tell me how to print above tree WITHOUT <ul> and <li>.
.
I want to print it using
intended for child and sub-child... because I have to put it in <select>
list and in other HTML tags
The structure I am using can be seen here: Getting a modified preorder tree traversal model (nested set) into a <ul>
with and- with for example?
– Martin Oct 02 '12 at 14:15