Hello all,
I have a huge task that I cannot find any good information on the internet about. I am working on a Hierarchical Data tree structure that will output the information into XML. I am working from 2 web pages, one is from MySQL: Managing Hierarchical Data in MySQL ( This is a good read and SitePoint: Storing Hierarchical Data in a Database (
The problem with most of these sites and already created classes is that the output from the PHP/Sql queries are in the form of tabbed indention's in HTML IE
What I am trying to do is return the structure in XML and that is getting confusing for me. I am wondering if I could get some help on this task. Since I have not seen this code done I will Open source it when I am done.
OK here is what I need help on. I read the article from
MySQL: Managing Hierarchical Data in MySQL ( and created the test database/table.
I have created a php file with the following code
The output looks like this:
This is not correct, I need to output to mach the tree structure like this
This tree structure came from Can someone help me create this tree?
Thanks for the read and help,
Timgerr
-How important does a person have to be before they are considered assassinated instead of just murdered?
So there you go! You're the retarded offspring of five monkeys having butt sex with a fish-squirrel! Congratulations!
I have a huge task that I cannot find any good information on the internet about. I am working on a Hierarchical Data tree structure that will output the information into XML. I am working from 2 web pages, one is from MySQL: Managing Hierarchical Data in MySQL ( This is a good read and SitePoint: Storing Hierarchical Data in a Database (
The problem with most of these sites and already created classes is that the output from the PHP/Sql queries are in the form of tabbed indention's in HTML IE
Code:
ELECTRONICS
TELEVISIONS
TUBE
LCD
PLASMA
PORTABLE ELECTRONICS
MP3 PLAYERS
FLASH
CD PLAYERS
2 WAY RADIOS
What I am trying to do is return the structure in XML and that is getting confusing for me. I am wondering if I could get some help on this task. Since I have not seen this code done I will Open source it when I am done.
OK here is what I need help on. I read the article from
MySQL: Managing Hierarchical Data in MySQL ( and created the test database/table.
I have created a php file with the following code
Code:
<?php
header("Content-type: text/xml");
if(!$dbconnect = mysql_connect('localhost', 'root')) {
echo "Connection failed to the host 'localhost'.";
exit;
} // if
if (!mysql_select_db('tree')) {
echo "Cannot connect to database 'test'";
exit;
} // if
$table_id = 'nested_category';
//$query = "SELECT * FROM $table_id";
$query = "SELECT node.name, node.lft, node.rgt
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND parent.name = 'ELECTRONICS'
ORDER BY node.lft";
$dbresult = mysql_query($query, $dbconnect);
// start with an empty $right stack
$right = array();
// This is the first line that is given
echo "<?xml version=\"1.0\"?>\n";
// process one row at a time
while($row = mysql_fetch_array($dbresult)) {
if($row['lft'] + 1 == $row['rgt']){
$show = $show . "<node label=\"" . $row['name'] . "\"></node>\n";
} else {
$show = $show . "<node label=\"" . $row['name'] . "\">\n";
}
if (count($right)>0) {
// check if we should remove a node from the stack
while ($right[count($right)-1]<$row['rgt']) {
array_pop($right);
$show = $show . "</node>\n";
}
}
$right[] = $row['rgt'];
}
echo "<root>\n" . $show . "</root>";
?>
The output looks like this:
Code:
<?xml version="1.0"?>
<root>
<node label="ELECTRONICS">
<node label="TELEVISIONS">
<node label="TUBE"></node>
<node label="LCD"></node>
</node>
<node label="PLASMA"></node>
</node>
<node label="PORTABLE ELECTRONICS">
</node>
</node>
<node label="MP3 PLAYERS">
<node label="FLASH"></node>
<node label="CD PLAYERS"></node>
</node>
</node>
<node label="2 WAY RADIOS"></node>
</node>
</root>
This is not correct, I need to output to mach the tree structure like this
Code:
<?xml version="1.0"?>
<root>
<node label="ELECTRONICS">
<node label="TELEVISIONS">
<node label="TUBE"></node>
<node label="LCD"></node>
<node label="PLASMA"></node>
</node>
<node label="PORTABLE ELECTRONICS">
<node label="MP3 PLAYERS">
<node label="FLASH"></node>
</node>
<node label="CD PLAYERS"></node>
<node label="2 WAY RADIOS"></node>
</node>
/node>
</root>
Thanks for the read and help,
Timgerr
-How important does a person have to be before they are considered assassinated instead of just murdered?
So there you go! You're the retarded offspring of five monkeys having butt sex with a fish-squirrel! Congratulations!