



ajax tree,… we need that if we have parent and child field data on database. and we showing this data to tree form. ok we have simple script to do that.
follow this instruction :
1. create javascript code and place to javascript directory
ajax.js
//
class.database.php
rows = 0;
if($_SERVER['HTTP_HOST'] == 'localhost' || ereg('^192\.168\.0\.[0-9]+$', $_SERVER['HTTP_HOST'])) {
/* Local connetion vars */
$this->host = "localhost";
$this->password = "root";
$this->user = "root";
$this->database = "database";
} else {
/* Internet connetion vars */
$this->host = "server";
$this->password = "password";
$this->user = "user";
$this->database = "database";
}
} // Method : end
function OpenLink() { // Method : begin
$this->link = @mysql_connect($this->host,$this->user,$this->password); // or die (print "Class Database: Error while connecting to DB (link)");
if(mysql_error()) {
$this->connected = false;
$this->error = mysql_error();
} else {
}
return $this->connected;
} // Method : end
function SelectDB() { // Method : begin
if(!@mysql_select_db($this->database,$this->link)) { //; or die (print "Class Database: Error while selecting DB");
$this->connected = false;
$this->error = mysql_error();
} else {
$this->connected = true;
}
} // Method : end
function CloseDB() { // Method : begin
mysql_close();
} // Method : end
function Query($query) { // Method : begin
$this->OpenLink();
$this->SelectDB();
$this->query = $query;
$this->result = mysql_query($query,$this->link) or die (print "Class Database: Error while executing Query");
$this->error = mysql_error();
// $rows=mysql_affected_rows();
if(ereg("SELECT",$query)) {
$this->rows = mysql_num_rows($this->result);
}
$this->CloseDB();
} // Method : end
// Return true if there was an error
function is_error() {
return (!empty($this->error)) ? true : false;
}
function fetchRow() { // Method : begin
return mysql_fetch_array($this->result);
$this->error = mysql_error();
} // Method : end
function getResult($row,$field) { // Method : begin
return mysql_result($this->result,$row,$field);
} // Method : end
function getError() { // Method : begin
return mysql_error();
} // Method : end
function getNumRows() { // Method : begin
return $this->rows;
} // Method : end
} // Class : end
?>
class.tree.php
<?phpid = $id;
$this->database = new Database();
if(!isset($this->id))
$this->id = 0;
}
// ********************** METODO LIST
function countSub($id) {
$sql = "SELECT COUNT($this->fieldId) AS subOptions FROM $this->table";
$sql.= " WHERE $this->fieldFrom = $id;";
//echo $sql;
$this->database->rows = $this->database->getResult($this->database->query($sql),"subOptions");
}
function listSub($id) {
$sql = "SELECT $this->fieldId,$this->fieldView FROM $this->table";
$sql.= " WHERE $this->fieldFrom = $id;";
//echo $sql;
return $this->database->query($sql);
}
function display() {
$this->listSub($this->id);
if($this->database->getNumRows() > 0) {
while($row=$this->database->fetchRow()) {
$tree_sub=new Tree($row[$this->fieldId]);
$tree_sub->countSub($row[$this->fieldId]);
echo "
";
echo (($tree_sub->database->getNumRows() > 0)?"fieldId].")\">[+]":"[-]");
echo " ".$row[$this->fieldView]."\n";
echo "
fieldId]."\" class=\"fld_ln\" style=\"display:none;\">
";
echo "
";
}
}
}
} // clase : fin
?>
3. create tree.php and tree_sub.php code
tree.php
/*
Class: Tree Class
Developed by: Roberto Morales Olivares | roberto@formatodigital.com
Creation date: 2 / Septiembre / 2007
Funtion(es) realizada(s): Demo tree.
Comments: This class generate a dynamic tree ajax directory of items retrieved from MySQL dabatase tables.
*/
?>
include("classes/class.tree.php");
?>
echo "fddddddddddddd".$_GET["id"];
$tree=new Tree(); // or $_POST["id"] numeric value of the root folder/category/directory to create the tree
$tree->display();
?>