...

View Full Version : help me to produce proper output.



chunk
09-18-2011, 01:15 PM
Hi,
this could be really simple for producing the output in html format by using the <ul> <li>

this is a tree traversal function and is working fine. But i want to display the output in proper <ul> and <li> format and im not able to put <ul> and <li> at proper place. please anyone help me to put these at proper place

here is the code



<?php

include("includes/connection.php");

traversal("user_1");
?>

<?php
$cnt_left = -1;
$cnt_right = -1;
$l ;
function traversal($root)
{
global $cnt_left;
global $cnt_right;
global $l;
$sql = "SELECT * FROM users where root_id= '$root' ";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);

if($root!="")
{

if($l == "L")
{
echo "<ul><li>";
}

if($l == "R")
{

//echo $root . " : $l ";
echo "</li>";
echo "</ul>";
echo "</li>";
echo "<li>";
// echo "</li>";
}

// echo "Root = ".$root;

echo $root . " : $l ";
if($row['left_flag'] > 0)
{
$cnt_left = $cnt_left +1;
$l ="L";


traversal($row['left_node']);

}
else
{

$cnt_left = $cnt_left +1;
$l ="R";

}
if($row['right_flag'] > 0)
{
$cnt_right = $cnt_right +1;


traversal($row['right_node']);
}
else
{

$cnt_right = $cnt_right +1;
}
}
else
{
echo "No Root";
}

}

?>

BluePanther
09-18-2011, 02:21 PM
I'm struggling to follow your tree structure. It looks kind of like a double-linked list, but I'm unsure. Can you describe your tree a bit more?

Any tree structure I've implemented has always carried an 'offset' parameter in the recursion - which holds a value for depth. Regardless, <ul><li> should be easy to implement. A new tree is started with <ul> with it's first node as <li>, so your recursion would check for a new node at this depth using whatever identifier you're using, if exists - echo/return <ul><li> current node + recursion </li></ul>; else - return empty string.

chunk
09-18-2011, 06:08 PM
I'm struggling to follow your tree structure. It looks kind of like a double-linked list, but I'm unsure. Can you describe your tree a bit more?

Any tree structure I've implemented has always carried an 'offset' parameter in the recursion - which holds a value for depth. Regardless, <ul><li> should be easy to implement. A new tree is started with <ul> with it's first node as <li>, so your recursion would check for a new node at this depth using whatever identifier you're using, if exists - echo/return <ul><li> current node + recursion </li></ul>; else - return empty string.

Hi BluePanther,
thank you very much for you reply, i have spent ages on searching on the internet for the solution but could not found the solution, thats y i created this thread.

And thank you for your interest in my problem.
my code displays the tree like the image below and that is incorrect display of this tree :-(

http://www.codingforums.com/attachment.php?attachmentid=10296&stc=1&d=1316624792



Here im writing all the info im using for generating the tree.

This is simple binary tree.

in the database table there is a field
my_node_id ( this is just the node ID of the current user )
root_id ( this field contain the id of its parent)
left_node ( this field contain the ID of the node that is assigned on the left side of the parent / node / this row )
right_node ( this field contain the ID of the node that is assigned on the right side of the parent / node / this row )

table



CREATE TABLE IF NOT EXISTS `tbl_users_temp` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`my_node_id` varchar(255) NOT NULL,
`left_flag` int(11) NOT NULL,
`right_flag` int(11) NOT NULL,
`root_id` varchar(255) NOT NULL,
`left_node` varchar(255) NOT NULL,
`right_node` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=41 ;

--
-- Dumping data for table `tbl_users_temp`
--

INSERT INTO `tbl_users_temp` (`id`, `name`, `password`, `my_node_id`, `left_flag`, `right_flag`, `root_id`, `left_node`, `right_node`) VALUES
(1, 'A', '1234', 'myuser00_1', 1, 1, '', 'myuser00_2', 'myuser00_3'),
(2, '2', '1234', 'myuser00_2', 1, 1, 'myuser00_1', 'myuser00_4', 'myuser00_6'),
(3, '3', '1234', 'myuser00_3', 1, 1, 'myuser00_1', 'myuser00_5', 'myuser00_7'),
(4, '4', '1234', 'myuser00_4', 1, 1, 'myuser00_2', 'myuser00_8', 'myuser00_12'),
(5, '5', '1234', 'myuser00_5', 1, 1, 'myuser00_3', 'myuser00_10', 'myuser00_14'),
(6, '6', '1234', 'myuser00_6', 1, 1, 'myuser00_2', 'myuser00_9', 'myuser00_13'),
(7, '7', '1234', 'myuser00_7', 1, 1, 'myuser00_3', 'myuser00_11', 'myuser00_15'),
(8, '8', '1234', 'myuser00_8', 1, 1, 'myuser00_4', 'myuser00_16', 'myuser00_24'),
(9, '9', '1234', 'myuser00_9', 1, 1, 'myuser00_6', 'myuser00_18', 'myuser00_26'),
(10, '10', '1234', 'myuser00_10', 1, 1, 'myuser00_5', 'myuser00_20', 'myuser00_28'),
(11, '11', '1234', 'myuser00_11', 1, 1, 'myuser00_7', 'myuser00_22', 'myuser00_30'),
(12, '12', '1234', 'myuser00_12', 1, 1, 'myuser00_4', 'myuser00_17', 'myuser00_25'),
(13, '13', '1234', 'myuser00_13', 1, 1, 'myuser00_6', 'myuser00_19', 'myuser00_27'),
(14, '14', '1234', 'myuser00_14', 1, 1, 'myuser00_5', 'myuser00_21', 'myuser00_29'),
(15, '15', '1234', 'myuser00_15', 1, 1, 'myuser00_7', 'myuser00_23', 'myuser00_31'),
(16, '16', '1234', 'myuser00_16', 1, 0, 'myuser00_8', 'myuser00_32', '0'),
(17, '17', '1234', 'myuser00_17', 1, 0, 'myuser00_12', 'myuser00_34', '0'),
(18, '18', '1234', 'myuser00_18', 1, 0, 'myuser00_9', 'myuser00_36', '0'),
(19, '19', '1234', 'myuser00_19', 0, 0, 'myuser00_13', '0', '0'),
(20, '20', '1234', 'myuser00_20', 1, 0, 'myuser00_10', 'myuser00_38', '0'),
(21, '21', '1234', 'myuser00_21', 0, 0, 'myuser00_14', '0', '0'),
(22, '22', '1234', 'myuser00_22', 0, 0, 'myuser00_11', '0', '0'),
(23, '23', '1234', 'myuser00_23', 0, 0, 'myuser00_15', '0', '0'),
(24, '24', '1234', 'myuser00_24', 1, 0, 'myuser00_8', 'myuser00_33', '0'),
(25, '25', '1234', 'myuser00_25', 1, 0, 'myuser00_12', 'myuser00_35', '0'),
(26, '26', '1234', 'myuser00_26', 1, 0, 'myuser00_9', 'myuser00_37', '0'),
(27, '27', '1234', 'myuser00_27', 0, 0, 'myuser00_13', '0', '0'),
(28, '28', '1234', 'myuser00_28', 1, 0, 'myuser00_10', 'myuser00_39', '0'),
(29, '29', '1234', 'myuser00_29', 0, 0, 'myuser00_14', '0', '0'),
(30, '30', '1234', 'myuser00_30', 0, 0, 'myuser00_11', '0', '0'),
(31, '31', '1234', 'myuser00_31', 0, 0, 'myuser00_15', '0', '0'),
(32, '32', '1234', 'myuser00_32', 0, 0, 'myuser00_16', '0', '0'),
(33, '33', '1234', 'myuser00_33', 0, 0, 'myuser00_24', '0', '0'),
(34, '34', '1234', 'myuser00_34', 0, 0, 'myuser00_17', '0', '0'),
(35, '35', '1234', 'myuser00_35', 0, 0, 'myuser00_25', '0', '0'),
(36, '36', '1234', 'myuser00_36', 1, 0, 'myuser00_18', 'myuser00_40', '0'),
(37, '37', '1234', 'myuser00_37', 0, 0, 'myuser00_26', '0', '0'),
(38, '38', '1234', 'myuser00_38', 0, 0, 'myuser00_20', '0', '0'),
(39, '39', '1234', 'myuser00_39', 0, 0, 'myuser00_28', '0', '0'),
(40, '40', '1234', 'myuser00_40', 0, 0, 'myuser00_36', '0', '0');



and here is the code



<?php
error_reporting(5);
include("includes/connection.php");
echo "<div id=\"TreeContainer\"><ul id=\"tree\"><li> ME";
traversal("myuser00_1");
echo "</li></ul></div>";
?>

<?php
$cnt_left = -1;
$cnt_right = -1;
$l ;
$i;
function traversal($root)
{
global $cnt_left;
global $cnt_right;
global $l;
global $i;
$sql = "SELECT * FROM tbl_users_temp where my_node_id= '$root' ";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
if($root!="")
{
if($l == "L")
{
print "<ul>\n";
print "<li>\n";
print "$row[name] $flag ";
print "</li>\n";

}

if($l == "R")
{
print "<li>\n";
print "$row[name] ";
print "</li>\n";

}
if($row['left_flag'] > 0)
{
$cnt_left = $cnt_left +1;
$l = "L";
traversal($row['left_node']);

}
else
{
$cnt_left = $cnt_left +1;
}
if($row['right_flag'] > 0)
{
$cnt_right = $cnt_right +1;
$l = "R";
traversal($row['right_node']);

}
else
{
$cnt_right = $cnt_right +1;
print "</ul>\n";
}
}
else
{
echo "No Root";
}

}

?>





this

BluePanther
09-19-2011, 07:29 AM
When you say incorrect display of the tree, have you checked the actual output order to make sure the tree is being outputted properly? Have you confirmed that you're getting closing tags all the way through, in the right order?

chunk
09-19-2011, 07:56 AM
When you say incorrect display of the tree, have you checked the actual output order to make sure the tree is being outputted properly? Have you confirmed that you're getting closing tags all the way through, in the right order?

hi BluePanther,
im getting all the nodes properly with the recursion, But im not able to produce the correct tree, Because im not able to put the opening and closing tags of UL and LI at proper place :-(
thats y i posted the code if you could find the proper place to put them :-|

thank you

BluePanther
09-19-2011, 08:13 AM
Ok. It's just when you post a screenshot and say 'this is wrong' without explanation, it leads me to validate what is actually wrong.

Show me two pages, both source and output, one with the recursion the way it was before, and one with your attempt at the list tags so I can see the difference.

chunk
09-19-2011, 08:52 AM
Ok. It's just when you post a screenshot and say 'this is wrong' without explanation, it leads me to validate what is actually wrong.

Show me two pages, both source and output, one with the recursion the way it was before, and one with your attempt at the list tags so I can see the difference.

Hi BluePanther,
i have already given the source on #3 with database, this is just a binary tree and nothing else :-)
the correct tree should like the image below :-)

http://www.codingforums.com/attachment.php?attachmentid=10298&stc=1&d=1316418703

BluePanther
09-19-2011, 09:33 AM
If you notice, I actually asked to see the code that displays the correct tree, but in the format you don't want - the code that produces the screen capture you provided in that reply.

Also, any particular reason why you're using print instead of echo? Not really important, just curious.

chunk
09-19-2011, 01:08 PM
If you notice, I actually asked to see the code that displays the correct tree, but in the format you don't want - the code that produces the screen capture you provided in that reply.

Also, any particular reason why you're using print instead of echo? Not really important, just curious.
hi BluePanter,
i also used this code on the URL below. And i integrated it with my database. The database i have already pasted in #3


http://snipplr.com/view/22864


Actually i can't understand the code written in this URL, that'y i can't modify this code.

and also this code need some modifications to display the desired user's tree in the database.

This code only displays the correct tree for first user in the database with this query


SELECT * FROM table


And the correct tree is produced by this code. But this only displays the tree of first user in the database. if i change the query to




SELECT * FROM table where id > 3


to view the tree of user whose id is 4.

then this code go to an infinite loop.

But at last i only want a code that produce the tree with my table structure. And i can't get anything useful :-(

If you are going to try this code, provided by above URL. Then these fields in database

left_node
right_node
root_id

must remove myusers00_ from each row. Only number after _ should be the value on each row for these three columns to work with this code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum