PHP Infinite Classification

Source: Internet
Author: User

PHP Infinite Classification

To create a database and tables:
Copy Code

CREATE DATABASE ' sortclass ' DEFAULT CHARSET UTF8;
CREATE TABLE IF not EXISTS ' class ' (
' CID ' mediumint (8) unsigned not NULL auto_increment,
' PID ' mediumint (8) unsigned not NULL,
' CNAME ' varchar not NULL,
PRIMARY KEY (' CID '),
KEY ' pid ' (' pid ')
) Engine=myisam DEFAULT Charset=utf8;

Working with file Examples:
PHP code
Copy Code

<?php
Header ("content-type:text/html; Charset=utf-8 ");
Connecting to a database
$link = mysql_connect (' localhost ', ' root ', ' Eric ') or Die (Mysql_error ());
mysql_select_db (' Sortclass ', $link);
Infinite Classification Class Library
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_assoc ($results)) {
$this->setnode ($node [' CID '], $node [' pid '], $node [' cname ']);
}
}
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) {
mysql_query ("INSERT INTO $this->table (' pid ', ' cname ') VALUES (' $pid ', ' $name ')", $this->link);
}
function Modnode ($cid, $newName) {
mysql_query ("Update $this->table set ' cname ' = ' $newName ' where ' cid ' = $cid", $this->link);
}
function Delnode ($cid) {
$allChilds = $this->getchilds ($cid);
$sql = ';
if (empty ($allChilds)) {
$sql = "Delete from $this->table where ' cid ' = $cid";
}else{
$sql = ' Delete from '. $this->table. ' WHERE ' CID ', implode (', ', $allChilds). ', '. $cid. ')
}
mysql_query ($sql, $this->link);
}
function MoveNode ($cid, $topid) {
mysql_query ("Update $this->table set ' pid ' = $topid where ' cid ' = $cid", $this->link);
}
}
Function
function back () {
Echo ' <script language= "java Script" >window.location.href= "test.php?" +new Date (). GetTime ();</script> ';
Exit
}
Sound into a 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, ' class ');
$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 ();
?>

CSS Styles
Copy Code

<style type= "Text/css" >
body{font-size:12px;}
Ul{list-style:none;}
A{cursor:pointer;}
</style>


JS Code

Copy Code
<script language= "java Script" >
function $ (e) {return document.getElementById (e);}
Function mod (CID) {
$ (' CID '). Value=cid;
$ (' op '). value= ' mod ';
$ (' name '). style.border= ' 1px solid red ';
}
</script>

HTML code Section
Copy Code

<form action= "test.php" method= "POST" >
Name: <input type= "text" id= "name" name= "CNAME"/> Added to: <?=makeselect ($category, ' pid ')? ><br/>
<input type= "hidden" id= "Op" name= "Op" value= "Add"/>
<input type= "hidden" id= "CID" name= "CID"/>
<input type= "Submit" value= "Submit"/>
</form>
<form action= "test.php" method= "POST" >
<?=makeselect ($category, ' who ')?> moved to: <?=makeselect ($category, ' to ')?>
<input type= "hidden" id= "Op" name= "Op" value= "Move"/>
<input type= "Submit" value= "Submit"/>
</form>
<ul>
<?php
foreach ($category as $id) {
Echo ' <li> ' $tree->getlayer ($id, ' | | '). $tree->getvalue ($id). ' <a href= ' test.php?op=del&cid= '. $ Id. ' " >Del</a> <a onclick= "mod ('. $id. ')" >Edit</a> </li> ';
}
?>
</ul>

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.