|
Class sortclass{
var $data = array ();
var $child = array ( -1=>array ());
var $layer = array ( -1=>-1);
var $parent = array ();
var $link;
var $table;
function Sortclass ($link, $table) {
$this->setnode (0,-1, ' top pole node ');
$this->link = $link;
$this->table = $table;
$node = Array ();
$results = mysql_query ("select * from $this->table", $this->link);
while ($node = Mysql_fetch_array ($results)) {
$this->setnode ($node [' id '], $node [' f_id '], $node [' name ']);
}
}
function Setnode ($id, $parent, $value) {
$parent = $parent? $parent: 0;
$this->data[$id] = $value;
$this->child[$id] = array ();
$this->child[$parent] = $id;
$this->parent[$id] = $parent;
$this->layer[$id] =!isset ($this->layer[$parent])? 0: $this->layer[$parent] + 1;
}
Function GetList (& $tree, $root = 0) {
foreach ($this->child[$root] as $key => $id) {
$tree [] = $id;
if ($this->child[$id]) $this->getlist ($tree, $id);
}
}
function GetValue ($id) {return $this->data[$id];}
function Getlayer ($id, $space = False) {
Return $space str_repeat ($space, $this->layer[$id]): $this->layer[$id];
}
function GetParent ($id) {return $this->parent[$id];}
function Getparents ($id) {
while ($this->parent[$id]!=-1) {
$id = $parent [$this->layer[$id]] = $this->parent[$id];
}
Ksort ($parent);
Reset ($parent);
return $parent;
}
function Getchild ($id) {return $this->child[$id];}
function Getchilds ($id = 0) {
$child = Array ($id);
$this->getlist ($child, $id);
return $child;
}
function AddNode ($name, $pid) {
echo "INSERT into $this->table (' f_id ', ' name ') VALUES (' $pid ', ' $name ')"; exit;
mysql_query ("INSERT INTO $this->table (' f_id ', ' name ') VALUES (' $pid ', ' $name ')", $this->link);
}
function Modnode ($cid, $newname) {
mysql_query ("Update $this->table set ' name ' = ' $newname ' where ' id ' = $cid", $this->link);
}
function Delnode ($cid) {
$allchilds = $this->getchilds ($cid);
$sql = ';
if (empty ($allchilds)) {
$sql = "Delete from $this->table where ' id ' = $cid";
}else{
$sql = ' Delete from '. $this->table. ' WHERE ' id '. Implode (', ', $allchilds). ', '. $cid. ') ';
}
mysql_query ($sql, $this->link);
}
function MoveNode ($cid, $topid) {
mysql_query ("Update $this->table set ' f_id ' = $topid where ' id ' = $cid", $this->link);
}
}
Function
function back () {
Echo ' <script language= ' web effects ' >window.location.href= ' news.class.php? +new date (). gettime ();</script> ';
Exit
}
Generate Select
function Makeselect ($array, $formname) {
Global $tree;
$select = ' <select name= '. $formname. ' " > ';
foreach ($array as $id) {
$select. = ' <option value= '. $id. ' > $tree->getlayer ($id, ' | | '). $tree->getvalue ($id). " </option> ";
}
return $select. ' </select> ';
}
$tree = new Sortclass ($link, ' p_newsclass ');
$op =!empty ($_post[' op '])? $_post[' op ']: $_get[' op '];
if (!empty ($op)) {
if ($op = = ' Add ') {
$tree->addnode ($_post[' cname '],$_post[' pid '));
Back ();
}
if ($op = = ' MoD ') {
$tree->modnode ($_post[' cid '],$_post[' cname '));
Back ();
}
if ($op = = ' del ') {
$tree->delnode ($_get[' CID '));
Back ();
}
if ($op = = ' Move ') {
$tree->movenode ($_post[' Who '],$_post[' to '));
Back ();
}
}
$category = $tree->getchilds ();
?>
|