Hi everyone...So I am new to this whole thing in php...of creating classes, and then instantiating objects.
So what I am doing pretty much is making a tree/explorer view for my website. I am just right now trying to add nodes, but I cant figure out how to access properties of my class object (sorry if my terms are wrong)
I guess my question is just: am i using the class right? instantiating node objects etc..? (you will see when you look at my code further down)
I have done similar things in java. Here is my class code:
<?php
class node{
public $child;
public $parent = "";
public $key = "";
public $data = "";
public $next;
public function __construct($k, $p, $d)
{
global $parent,$key,$data,$next;
$parent = $p;
$key = $k;
$data = $d;
}
public function setParent($p)
{
global $parent;
$parent = $p;
}
public function getParent()
{
global $parent;
return $parent;
}
public function setChild($c)
{
global $child;
$child = $c;
}
public function getChild()
{
global $child;
return $child;
}
public function setData($d)
{
global $data;
$data = $d;
}
public function getData()
{
global $data;
return $data;
}
public function setNext($n)
{
global $next;
$next = $n;
}
public function getNext()
{
global $next;
if($next == null){
return $next;
}else{
return null;
}
}
public function setKey($k)
{
global $key;
$key = $k;
}
public function getKey()
{
global $key;
return $key;
}
}
?>
right now I am just trying to use the getNext() function (it used to just say global next; return $next;..but that gave me errors).
It is being called by:
if($head->getNext() == null){
$head->setNext($newNode);
echo "1";
}
so what I am trying to do is say that if the head node (points to the first node...i think 😛) then make head point to the node just created.
All of this is inside a while loop, and when i try running it i just get a line of 1s across the screen (it is execuated 375 times)
for a bigger look at what my code is doing, here (if you are interested, ps. this isnt the complete code. i do not get any syntax errors, the problem is that the getnext is never not null...):
$query = "SELECT * FROM `pdem_specifications`.`tblSpecs` ORDER BY `tblSpecs`.`TreeViewID` ASC";
$result = mysql_query($query);
$num = mysql_num_rows($result);
$keyPart = "";
$keyT1 = "";
$keyT2 = "";
$keyT3 = "";
$keyT4 = "";
$a=0;
$b=0;
$c=0;
$d=0;
$e=0;
$i=0;
$head = new node("head",null,null);
while($i < $num){
if(mysql_result($result,$i,"Part")!=null){
$newNode = new node("pa".$a,null,mysql_result($result,$i,"Part"));
$a++;
}elseif(mysql_result($result,$i,"Tier1")!=null){
$newNode = new node("t1".$b,"p".$a,mysql_result($result,$i,"Tier1"));
$b++;
}elseif(mysql_result($result,$i,"Tier2") !=null){
$newNode = new node("t2".$c,"t1".$b,mysql_result($result,$i,"Tier2"),null);
$c++;
}elseif(mysql_result($result,$i,"Tier3") !=null){
$newNode = new node("t3".$d,"t2".$c,mysql_result($result,$i,"Tier3"));
$d++;
}elseif(mysql_result($result,$i,"Tier4") !=null){
$newNode = new node("t4".$e,"t3".$d,mysql_result($result,$i,"Tier4"));
$e++;
}
/*if($newNode->getParent()==null){
echo $newNode->getData();
echo "<br/>";
}*/
if($head->getNext() == null){
$head->setNext($newNode);
echo "1";
}
thanks a lot!