Php+mysql Database Infinite Classification code (1/2)

Source: Internet
Author: User
Tags foreach

Connecting to a database
$link = mysql_connect (' localhost ', ' root ', ') ' or Die (mysql_error ());
mysql_select_db (' class ', $link) or Die (Mysql_error ());
mysql_query ("Set names GBK");
Infinite Classification Class Library

The code is as follows Copy Code

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 ();
?>

Home 1 2 last page
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.