class initialising itself over and over again?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LoCal
Forum Newbie
Posts: 3
Joined: Mon Sep 01, 2003 4:22 am
Location: Germany

class initialising itself over and over again?

Post by LoCal »

Hei,

I got a wiered problem. I try to make a tree-like-structure. The nodes having an array for the childs. My prob is now that when the first child will be inserted the amount of elements in the array is 0 before and 1 after the insertion... which is quite ok... but now.. when the next one is going to be inserted, the size of the array is again 0 and 1 after.

Can anyone help me? My code is below.

Big thx in advance.

Peace,
LoCal

The classes:
baumschule.php

Code: Select all

<?php
//$CR="\n\r";
$CR="<br />";
//session_register("uid");
$uid=39;
$first=true;

include("../phpdb.php");
include("classes.php");
echo("<b>Chili:</b> Freust du dich auf die Tour mit dem neuen Straßenflitzer 2.8?".$CR.
	 "<b>Bernd:</b> Lass mich überlegen - nein!".$CR.$CR);
$rs = ibase_query($intranet, "SELECT * FROM favoriten_ordner WHERE UID=".$uid."ORDER BY ID");
while($row = ibase_fetch_object($rs))&#123;
	if($first) &#123;
		$tree&#1111;0]=new node;
		$tree&#1111;0]->setID($row->ID);
		$tree&#1111;0]->setMID($row->MASTER);
		$tree&#1111;0]->setName($row->NAME);
		$first=false;
	&#125; else &#123;
		array_push($tree, new node);
		$tree&#1111;count($tree)-1]->setID($row->ID);
		$tree&#1111;count($tree)-1]->setMID($row->MASTER);
		$tree&#1111;count($tree)-1]->setName($row->NAME);
		processActNode($tree, $tree&#1111;count($tree)-1]); 
	&#125;
&#125;

function processActNode(&$tree, &$actnode) &#123;
		$parent=findParent($tree, $actnode->getMID());
		$actnode->setParent($parent);
		$parent->addChild($actnode);
	
&#125;

function findParent(&$tree, $mid) &#123;
	for($i=0;$i<count($tree);$i++) if($tree&#1111;$i]->getID()==$mid) return $tree&#1111;$i];	
&#125;

echo($CR."<table><tr bgcolor='#dde1ea' align='center'><td align='center'>Name</td><td align='center'>ID</td><td>Master</td><td align='center'>AnzahlKinder</td><td align='center'>Eltern</td></tr>");
for($i=0;$i<count($tree);$i++) echo("<tr><td align='center'>".$tree&#1111;$i]->getName()."</td><td align='center'> ".$tree&#1111;$i]->getID()."</td><td align='center'>".$tree&#1111;$i]->getMID()."</td><td align='center'>".$tree&#1111;$i]->getAmountOfChildren()." </td><td align='center'>".$tree&#1111;$i]->getParent()."</td></tr>");
echo("</table>".$CR);
?>
classes.php

Code: Select all

<?php

class node &#123;

	var $id=-1;
	var $mid=-1;
	var $parent;
	var $children;
	var $name;
	var $isLink=true;
	
	
	function setId($id) &#123;
		$this->id = $id;
	&#125;
	
	function setMid($mid) &#123;
		$this->mid = $mid;
	&#125;

	function setParent(&$parent) &#123;		
		$this->parent = $parent;
	&#125;

	function addChild(&$child) &#123;
		if(count($this->children)<=0)$this->children&#1111;0]=$child;
		else array_push($this->children,$child);
	&#125; 
	
	
	function setName($name) &#123;
		$this->name = $name;
	&#125;
	
	function setIsLink($isLink) &#123;
		$this->isLink = $isLink;
	&#125;

	function getID() &#123;
		return $this->id;
	&#125;
	
	function getMID()&#123;
		return $this->mid;
	&#125;
	
	function getParent() &#123;
		return $this->parent;
	&#125;
	
	function getChildren() &#123;
		return $this->children;
	&#125;
	
	function getAmountOfChildren() &#123;
		return count($this->children);
	&#125;
	
	function getChild($i) &#123;
		return $this->children&#1111;$i];
	&#125;
	
	function getName() &#123;
		return $this->name;
	&#125;
	function isLink() &#123;
		return $this->isLink;
	&#125;
&#125;

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

can't reproduce the error here, but wouldn't it be simpler this way?

Code: Select all

function findParent(&$tree, $mid) {
	for($i=0;$i<count($tree);$i++) if($tree[$i]->getID()==$mid) return $tree[$i];
	return NULL;
} 

function processActNode(&$tree, &$actnode) {
	$parent=findParent($tree, $actnode->getMID());
	$actnode->setParent($parent);
	if (!is_null($parent))
		$parent->addChild($actnode);
}
[...]
$tree = array();
while($row = ibase_fetch_object($rs)){
	$node = new node;
	$node->setID($row->ID);
	$node->setMID($row->MASTER);
	$node->setName($row->NAME);
	$tree[] = &$node;
	processActNode($tree, $node);
}
LoCal
Forum Newbie
Posts: 3
Joined: Mon Sep 01, 2003 4:22 am
Location: Germany

Post by LoCal »

volka wrote:can't reproduce the error here, but wouldn't it be simpler this way?
Do you mean it worked on your server?

Peace,

LoCal
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it appeared so.
LoCal
Forum Newbie
Posts: 3
Joined: Mon Sep 01, 2003 4:22 am
Location: Germany

Post by LoCal »

volka wrote:it appeared so.
Hmmm... could it be the php-version that it not works here??
Post Reply