0

Hi All Friends,
I Have a Problem in My PHP Code that The $tree Array not Store Third Value(Parent ID). my Code is Display Below. The Below code Show Parent/Child Tree from MySQL Database.:

dhtmlgoodies_tree.class.php

<?php

/*

This is one of the free scripts from www.dhtmlgoodies.com
You are free to use this script as long as this copyright message is kept intact

(c) Alf Magne Kalleland, http://www.dhtmlgoodies.com - 2005

*/

class dhtmlgoodies_tree{
	
	var $elementArray = array();
	var $nameOfCookie = "dhtmlgoodies_expanded"; // Name of the cookie where the expanded nodes are stored.
	
	function dhtmlgoodies_tree()
	{
		
		
	}

	function writeCSS()
	{
		?>
		<style type="text/css">
		/*
		
		This is one of the free scripts from www.dhtmlgoodies.com
		You are free to use this script as long as this copyright message is kept intact
		
		(c) Alf Magne Kalleland, http://www.dhtmlgoodies.com - 2005
		
		*/		
		#dhtmlgoodies_tree li{
			list-style-type:none;	
			font-family: arial;
			font-size:11px;
		}
		#dhtmlgoodies_topNodes{
			margin-left:0px;
			padding-left:0px;
		}
		#dhtmlgoodies_topNodes ul{
			margin-left:20px;
			padding-left:0px;
			display:none;
		}
		#dhtmlgoodies_tree .tree_link{
			line-height:13px;
			padding-left:2px;

		}
		#dhtmlgoodies_tree img{
			padding-top:2px;
		}
		#dhtmlgoodies_tree a{
			color: #000000;
			text-decoration:none;
		}
		.activeNodeLink{
			background-color: #316AC5;
			color: #FFFFFF;
			font-weight:bold;
		}
		</style>		
		<?php		
	}
	
	function writeJavascript()
	{
		?>
		<script type="text/javascript">
/************************************************************************************************************
Folder tree - PHP
Copyright (C) 2005 - 2009  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2005 - 2009
Owner of DHTMLgoodies.com
	
************************************************************************************************************/	
	
		var plusNode = 'images/dhtmlgoodies_plus.gif';
		var minusNode = 'images/dhtmlgoodies_minus.gif';
		
		var nameOfCookie = '<?php echo $this->nameOfCookie; ?>';
		<?php
		$cookieValue = "";
		if(isset($_COOKIE[$this->nameOfCookie]))$cookieValue = $_COOKIE[$this->nameOfCookie];		
		echo "var initExpandedNodes =\"".$cookieValue."\";\n";
		?>		
		/*
		These cookie functions are downloaded from 
		http://www.mach5.com/support/analyzer/manual/html/General/CookiesJavaScript.htm
		*/
		function Get_Cookie(name) { 
		   var start = document.cookie.indexOf(name+"="); 
		   var len = start+name.length+1; 
		   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
		   if (start == -1) return null; 
		   var end = document.cookie.indexOf(";",len); 
		   if (end == -1) end = document.cookie.length; 
		   return unescape(document.cookie.substring(len,end)); 
		} 
		// This function has been slightly modified
		function Set_Cookie(name,value,expires,path,domain,secure) { 
			expires = expires * 60*60*24*1000;
			var today = new Date();
			var expires_date = new Date( today.getTime() + (expires) );
		    var cookieString = name + "=" +escape(value) + 
		       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
		       ( (path) ? ";path=" + path : "") + 
		       ( (domain) ? ";domain=" + domain : "") + 
		       ( (secure) ? ";secure" : ""); 
		    document.cookie = cookieString; 
		} 
		/*
		End downloaded cookie functions
		*/
		
		function expandAll()
		{
			var treeObj = document.getElementById('dhtmlgoodies_tree');
			var images = treeObj.getElementsByTagName('IMG');
			for(var no=0;no<images.length;no++){
				if(images[no].className=='tree_plusminus' && images[no].src.indexOf(plusNode)>=0)expandNode(false,images[no]);
			}
		}
		function collapseAll()
		{
			var treeObj = document.getElementById('dhtmlgoodies_tree');
			var images = treeObj.getElementsByTagName('IMG');
			for(var no=0;no<images.length;no++){
				if(images[no].className=='tree_plusminus' && images[no].src.indexOf(minusNode)>=0)expandNode(false,images[no]);
			}
		}
		
		
		function expandNode(e,inputNode)
		{
			if(initExpandedNodes.length==0)initExpandedNodes=",";
			if(!inputNode)inputNode = this; 
			if(inputNode.tagName.toLowerCase()!='img')inputNode = inputNode.parentNode.getElementsByTagName('IMG')[0];	
			
			var inputId = inputNode.id.replace(/[^\d]/g,'');			
			
			var parentUl = inputNode.parentNode;
			var subUl = parentUl.getElementsByTagName('UL');

			if(subUl.length==0)return;
			if(subUl[0].style.display=='' || subUl[0].style.display=='none'){
				subUl[0].style.display = 'block';
				inputNode.src = minusNode;
				initExpandedNodes = initExpandedNodes.replace(',' + inputId+',',',');
				initExpandedNodes = initExpandedNodes + inputId + ',';
				
			}else{
				subUl[0].style.display = '';
				inputNode.src = plusNode;	
				initExpandedNodes = initExpandedNodes.replace(','+inputId+',',',');			
			}
			Set_Cookie(nameOfCookie,initExpandedNodes,60);
			
			
			
		}
		
		function initTree()
		{
			// Assigning mouse events
			var parentNode = document.getElementById('dhtmlgoodies_tree');
			var lis = parentNode.getElementsByTagName('LI'); // Get reference to all the images in the tree
			for(var no=0;no<lis.length;no++){
				var subNodes = lis[no].getElementsByTagName('UL');
				if(subNodes.length>0){
					lis[no].childNodes[0].style.visibility='visible';	
				}else{
					lis[no].childNodes[0].style.visibility='hidden';
				}
			}	
			
			var images = parentNode.getElementsByTagName('IMG');
			for(var no=0;no<images.length;no++){
				if(images[no].className=='tree_plusminus')images[no].onclick = expandNode;				
			}	

			var aTags = parentNode.getElementsByTagName('A');
			var cursor = 'pointer';
			if(document.all)cursor = 'hand';
			for(var no=0;no<aTags.length;no++){
				aTags[no].onclick = expandNode;		
				aTags[no].style.cursor = cursor;		
			}
			var initExpandedArray = initExpandedNodes.split(',');

			for(var no=0;no<initExpandedArray.length;no++){
				if(document.getElementById('plusMinus' + initExpandedArray[no])){
					var obj = document.getElementById('plusMinus' + initExpandedArray[no]);	
					expandNode(false,obj);
				}
			}				
		}
		
		window.onload = initTree;
		
		</script>	
		<?php
		
	}
	
	
	
	/*
	This function adds elements to the array
	*/
	
	function addToArray($id,$name,$parentID,$url="",$target="",$imageIcon="images/dhtmlgoodies_folder.gif"){
		if(empty($parentID))$parentID=0;	
		$this->elementArray[$parentID][] = array($id,$name,$url,$target,$imageIcon);
	}
	
	function drawSubNode($parentID){
		if(isset($this->elementArray[$parentID])){			
			echo "<ul>";
			for($no=0;$no<count($this->elementArray[$parentID]);$no++){
				$urlAdd = "";
				if($this->elementArray[$parentID][$no][2]){
					$urlAdd = " href=\"".$this->elementArray[$parentID][$no][2]."\"";
					if($this->elementArray[$parentID][$no][3])$urlAdd.=" target=\"".$this->elementArray[$parentID][$no][3]."\"";	
				}
				echo "<li class=\"tree_node\"><img class=\"tree_plusminus\" id=\"plusMinus".$this->elementArray[$parentID][$no][0]."\" src=\"images/dhtmlgoodies_plus.gif\"><img src=\"".$this->elementArray[$parentID][$no][4]."\"><a class=\"tree_link\"$urlAdd>".$this->elementArray[$parentID][$no][1]."</a>";	
				$this->drawSubNode($this->elementArray[$parentID][$no][0]);
				echo "</li>";
			}			
			echo "</ul>";			
		}		
	}
	
	function drawTree(){
		echo "<div id=\"dhtmlgoodies_tree\">";
		echo "<ul id=\"dhtmlgoodies_topNodes\">";
		for($no=0;$no<count($this->elementArray[0]);$no++){
			$urlAdd = "";
			if($this->elementArray[0][$no][2]){
				$urlAdd = " href=\"".$this->elementArray[0][$no][2]."\"";
				if($this->elementArray[0][$no][3])$urlAdd.=" target=\"".$this->elementArray[0][$no][3]."\"";	
			}
			echo "<li class=\"tree_node\" id=\"node_".$this->elementArray[0][$no][0]."\"><img id=\"plusMinus".$this->elementArray[0][$no][0]."\" class=\"tree_plusminus\" src=\"images/dhtmlgoodies_plus.gif\"><img src=\"".$this->elementArray[0][$no][4]."\"><a class=\"tree_link\"$urlAdd>".$this->elementArray[0][$no][1]."</a>";		
			$this->drawSubNode($this->elementArray[0][$no][0]);
			echo "</li>";	
		}	
		echo "</ul>";	
		echo "</div>";	
	}
}


?>

index.php

<?php

include("dhtmlgoodies_tree.class.php");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Tree View Test</title>
<style type="text/css">
	a{
		text-decoration:none;
		font-family:arial;
		font-size:0.8em;
	}
	</style>
</head>

<body onload="expandAll();return false">
<a href="#" onclick="expandAll();return false">Expand all nodes</a><br>
<a href="#" onclick="collapseAll();return false">Collapse all nodes</a><br>
<?PHP
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost 
$db_user = "user"; // change to your database password 
$db_password = "password"; // change to your database password 
$database = "database"; // provide your database name 

# STOP HERE 
#################################################################### 
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db); //Database Connect

function buildTree($id=null) {

	//make sure we can use our arrays outside of the function
	if($id == null) {
            $new_id = $_GET['id'];
            $parentID = $new_id;
     }
        $sql = "SELECT id,name,parentID FROM table_name WHERE parentID= $parentID";
 		global $tree;
	$tree = new dhtmlgoodies_tree();
        // Execute the query and go through the results.
        $result = mysql_query($sql);
        if($result)
        {
            while($row = mysql_fetch_array($result))
            {
                // Create a child array for the current ID
                $currentID = $row['id'];
                $name = $row['name'];
                $pid = $row['parentID'];
                // Print all children of the current ID
                $tree->addToArray($currentID,$name,$pid,"","_blank");
            }
        }
        else {
            die("Failed to execute query! ($level / $parentID)");
        }
}

buildTree ($id);

$tree->writeCSS();
$tree->writeJavascript();
$tree->drawTree();


?>
</body>
</html>

in the above Code Line: "$tree->addToArray($currentID,$name,$pid,"","_blank");"
Not Store Third Array Value.
Please Help ME.
My English is Poor so I Hope you all are Understand it.
Thanx...

4
Contributors
24
Replies
26
Views
7 Years
Discussion Span
Last Post by phpbeginners
0

Please Help Me.
It's very Urgent...
I am in Trouble.

$pid = $row['parentID'];

you said that you are not getting $pid value. change $pid name if use pid already in the file it willnot store value change it's name try once .
and also check where parentId have the value or not in db. check parentIDN name in db also.

Edited by rajabhaskar525: n/a

0

Please Help Me.
It's very Urgent...
I am in Trouble.

Ho, ho, ho. Bit off a bit more than you could chew, did you? :icon_lol:

You getting paid for this or is it an assignment?

0
$pid = $row['parentID'];

you said that you are not getting $pid value. change $pid name if use pid already in the file it willnot store value change it's name try once .
and also check where parentId have the value or not in db. check parentIDN name in db also.

No Friend.
This is Getting pid from Database but Not Store in Array witch is on Line #56 in index.php File.

0

Ho, ho, ho. Bit off a bit more than you could chew, did you? :icon_lol:

You getting paid for this or is it an assignment?

This is Tree Structure and i am a BCA final Year Studen and Showing in my Assignment. but i am only left 2 Days and I Don't Understand what i do. This Not Store $pid in Array.

0

before 56line , print $pid , check is it have the value or not . check it and trace it. don't worry friend evearything has the solution be cool.

0

before 56line , print $pid , check is it have the value or not . check it and trace it. don't worry friend evearything has the solution be cool.

I am Checking all Thing but Not Success. The $pid value Print Successfully outsider Array but Not Store in Array. Remaining all Values Store in Array.

0

$pid value print in array and dhtmlgoodies.class.ph file. i cut and paste your code , it works fine, but expande and collapse. check your code carefuully
my code

<?php
   include("dhtmlgoodies_tree.class.php");
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <title>Tree View Test</title>
 <style type="text/css">
  a{
text-decoration:none;
 font-family:arial;
font-size:0.8em;
 }
 </style>
 </head>
 <body onLoad="expandAll();return false">
  <a href="#" onClick="expandAll();return false">Expand all nodes</a><br>
  <a href="#" onClick="collapseAll();return false">Collapse all nodes</a><br>
 <?PHP
  $hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
  $db_user = "root"; // change to your database password
 $db_password = "sa"; // change to your database password
  $database = "raju"; // provide your database name
  
 # STOP HERE
  ####################################################################
 # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
  $db = mysql_connect($hostname, $db_user, $db_password);
  mysql_select_db($database,$db); //Database Connect
 function buildTree($id=null) {
 //make sure we can use our arrays outside of the function
  if($id == null) {
$new_id = $_GET['id'];
 $parentID = $new_id;
 }
 $sql = "SELECT * FROM userregistration WHERE auto_no=1";
 global $tree;
  $tree = new dhtmlgoodies_tree();
  // Execute the query and go through the results.
  $result = mysql_query($sql);
  if($result)
  {
 while($row = mysql_fetch_array($result))
 {
 // Create a child array for the current ID
$currentID = $row['auto_no'];
 $name = $row['firstname'];
 $pid = $row['email'];
  // Print all children of the current ID
  
 $tree->addToArray($currentID,$name,$pid,"","_blank");
 
 }
 }
 else {
 die("Failed to execute query! ($level / $parentID)");
 }
 }
 buildTree ($id);
 $tree->writeCSS();
  $tree->writeJavascript();
  $tree->drawTree();
 ?>
 </body>
 </html>
0

Thanx for Your Help but The above Posted Code is for Tree Printing. if There are not Store $pid value in Array The Tree Sould Work but The Expend Collapse Feature not Working and Not Show + sign before Parent Node. so for This Displaying you need to assign $pid.

0

Your Given Link i Try also but i want to Print tree from mySQL database in PHP.
and also Thanx for Your all Help...

0

see this link, in that see left panel having tree, do you want like that, if so post your database also, and how many levels do you want to display tree nodes, my link contain tree levels.

0

Yes i want the tree like http://www.taslan.ca/.
Database Schma

id           Name               parentID
-------------------------------------------
1            ABC                  0
2            BCD                  1
3            DEF                   1
4            EFG                   1
5            FGH                   2
and so on............

in This Tree there are Unlimited Level......
because in the above posted code the tree show unlimited level..

Edited by hemgoyal_1990: n/a

0

tick tock, tick tock

I'll have a look later, busy at the minute. If + not showing up, is it due to CSS/JS issues or php DB retrieval?

0

+ not Show Because the Parent ID is not Stored in array.
there are not any css or php issue.
and the above code retrive all child node.

0

Try changing to this:

$sql = "SELECT id,name,parentID FROM labels WHERE parentID= '$parentID'";

Note the single quote marks around parentID.

Also check that these images:

var plusNode = 'images/dhtmlgoodies_plus.gif';
var minusNode = 'images/dhtmlgoodies_minus.gif';

and

echo "<li class=\"tree_node\"><img class=\"tree_plusminus\" id=\"plusMinus".$this->elementArray[$parentID][$no][0]."\" src=\"images/dhtmlgoodies_plus.gif\"><img src=\"".$this->elementArray[$parentID][$no][4]."\"><a class=\"tree_link\"$urlAdd>".$this->elementArray[$parentID][$no][1]."</a>";

and

echo "<li class=\"tree_node\" id=\"node_".$this->elementArray[0][$no][0]."\"><img id=\"plusMinus".$this->elementArray[0][$no][0]."\" class=\"tree_plusminus\" src=\"images/dhtmlgoodies_plus.gif\"><img src=\"".$this->elementArray[0][$no][4]."\"><a class=\"tree_link\"$urlAdd>".$this->elementArray[0][$no][1]."</a>";		
			$this->drawSubNode($this->elementArray[0][$no][0]);

exist in the right location. You may need to make the urls (src) absolute.

0

Try changing to this:

$sql = "SELECT id,name,parentID FROM labels WHERE parentID= '$parentID'";

Note the single quote marks around parentID.

Also check that these images:

var plusNode = 'images/dhtmlgoodies_plus.gif';
var minusNode = 'images/dhtmlgoodies_minus.gif';

and

echo "<li class=\"tree_node\"><img class=\"tree_plusminus\" id=\"plusMinus".$this->elementArray[$parentID][$no][0]."\" src=\"images/dhtmlgoodies_plus.gif\"><img src=\"".$this->elementArray[$parentID][$no][4]."\"><a class=\"tree_link\"$urlAdd>".$this->elementArray[$parentID][$no][1]."</a>";

and

echo "<li class=\"tree_node\" id=\"node_".$this->elementArray[0][$no][0]."\"><img id=\"plusMinus".$this->elementArray[0][$no][0]."\" class=\"tree_plusminus\" src=\"images/dhtmlgoodies_plus.gif\"><img src=\"".$this->elementArray[0][$no][4]."\"><a class=\"tree_link\"$urlAdd>".$this->elementArray[0][$no][1]."</a>";		
			$this->drawSubNode($this->elementArray[0][$no][0]);

exist in the right location. You may need to make the urls (src) absolute.

I Have Made all Change from Your Guidance but The Problem is Still. and me also Change the Code.

index.php

<?PHP
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost 
$db_user = "ldbname"; // change to your database password 
$db_password = "password"; // change to your database password 
$database = "database"; // provide your database name 

# STOP HERE 
#################################################################### 
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db); //Database Connect

$parentid = $_GET['id']; // assuming that 0 is the main category.

get_sub_cats($parentid);

function get_sub_cats($parentid) {

$sql = "SELECT id,name,parentID FROM tbl_name WHERE parentID = ".$parentid.""; 
$run = mysql_query($sql);
global $tree;
$tree = new dhtmlgoodies_tree();	// Creating new tree object

while ($rec = mysql_fetch_assoc($run)) { 
$currentID = $rec['id'];
				$name = $rec['name'];
                $pid = $rec['parentID'];
                // Print all children of the current ID
				
				$tree->addToArray($currentID,$name,$pid,"","_blank");
get_sub_cats($rec['id']);

}

}

get_sub_cats($parentid);
$tree->writeCSS();
$tree->writeJavascript();
$tree->drawTree();

?>
0

Database Schema:

-------------------------------------------------------------------------
id			name			parentID
-------------------------------------------------------------------------
1			ABC			0
2			BCD			1
3			DEF			1
4			GHI			1
5			JKL			1
6			MNO			2
7			PQR			2
8			STU			2
9			VWX			3
10			YZX			3
11			MNO			3
12			JKL			4
13			Hemant			4
14			Abhi			5
15			Sanjay			5
16			Pankaj			10
17			MMM			15
18			AAA			3
19			BBB			1
20			CCC			8
21			DDD			8
22			EEE			8
23			FFF			7
24			GGG			7
25			HHH			6
26			III			5
27			JJJ			22
0

Database Schema:

-------------------------------------------------------------------------
id			name			parentID
-------------------------------------------------------------------------
1			ABC			0
2			BCD			1
3			DEF			1
4			GHI			1
5			JKL			1
6			MNO			2
7			PQR			2
8			STU			2
9			VWX			3
10			YZX			3
11			MNO			3
12			JKL			4
13			Hemant			4
14			Abhi			5
15			Sanjay			5
16			Pankaj			10
17			MMM			15
18			AAA			3
19			BBB			1
20			CCC			8
21			DDD			8
22			EEE			8
23			FFF			7
24			GGG			7
25			HHH			6
26			III			5
27			JJJ			22
--
-- Table structure for table `mytree`
--

CREATE TABLE IF NOT EXISTS `mytree` (
  `id` char(3) NOT NULL default '',
  `name` varchar(10) NOT NULL default '',
  `parentID` char(3) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `mytree`
--

INSERT INTO `mytree` (`id`, `name`, `parentID`) VALUES
('1', 'ABC', '0'),
('2', 'BCD', '1'),
('3', 'DEF', '1'),
('4', 'GHI', '1'),
('5', 'JKL', '1'),
('6', 'MNO', '2'),
('7', 'PQR', '2'),
('8', 'STU', '2'),
('9', 'VWX', '3'),
('10', 'YZX', '3'),
('11', 'MNO', '3'),
('12', 'JKL', '4'),
('13', 'Hemant', '4'),
('14', 'Abhi', '5'),
('15', 'Sanjay', '5'),
('16', 'Pankaj', '10'),
('17', 'MMM', '15'),
('18', 'AAA', '3'),
('19', 'BBB', '1'),
('20', 'CCC', '8'),
('21', 'DDD', '8'),
('22', 'EE', '8'),
('23', 'FFF', '7'),
('24', 'GGG', '7'),
('25', 'HHH', '6'),
('26', 'III', '5'),
('27', 'JJJ', '22');

your code is working.
Is this what you want?
http://www.momococo.com/daniweb/index.php
I just create temporary folder for that on my web.

0

if we select whole table then the code is work but when we select the data with recursion function this will not work this only store first two variable in database ie $id,$name or $id,$parentID.
You may Tested it from the code posted on first page.
this will give u error.....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.