Create a database tutorial and tables:
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
Header ("content-type:text/html; Charset=utf-8 ");
Connecting to a database
$link = MySQL tutorial _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= "javascript tutorial" >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 ();
?>
<style type= "Text/css Tutorial" >
body{font-size:12px;}
Ul{list-style:none;}
A{cursor:pointer;}
</style>
<script language= "JavaScript" >
function $ (e) {return document.getElementById (e);}
Function mod (CID) {
$ (' CID '). Value=cid;
$ (' op '). value= ' mod ';
$ (' name '). style.border= ' 1px solid red ';
}
</script>
<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>
Simple recursive query plus infinite classification of directory path field
Disadvantage: Query the database too many times, not convenient for other operations, such as the deletion of nodes. Add nodes, move nodes
2, left-right value infinite classification, pre-sorted binary tree
Disadvantages: Cumbersome operation, database redundancy, and add delete changes to the left and right value update
The algorithm of Fang-Xin with recursive non-infinite paging class
function Cate_arr ($arr,& $arr 2=array (), $per _id=0, $lv =0) {
static $i = 0; Starting from 0
if ((bool) $arr) {
foreach ($arr as $value) {
if ($value [' per_id ']== $per _id) {
$value [' LV ']= $lv;
$arr 2[$i]= $value;
$i + +;
$LV + +;
Cate_arr ($arr, $arr 2, $value [' cate_id '], $lv-);
}
}
}
}
Data structures such as
Array
0=>array (
' cate_id ' = 11,
' per_id ' = 0,
' Cate_name ' = ' a '
),
1=>array (
' cate_id ' = 12,
' per_id ' = 0,
' Cate_name ' = ' B '
),
2=>array (
' cate_id ' = 13,
' per_id ' = 11,
' Cate_name ' = ' A10 '
),
....);
Let's look at a simple and mysqli direct connection to the infinite classification
Header ("Content-type:text/html;charset=utf-8"); $db =new mysqli ("localhost", "root", "", "news_php100"); Instantiates a database connection. Make sure that you have loaded the Mysqli class library before using this, or connect it in mysql_connect. if (Mysqli_connect_errno ()) { echo "link failed:". Mysqli_connect_error (); exit ();} $db->query ("Set names UTF8"); $result = $db->query ("Select name from class where f_id=0"); Find the classification of f_id=0, which is to find each large class. while ($row = $result->fetch_assoc ()) { echo $row [' name ']. " < br> "; This loops out every big class. } //Also we can recycle the news from the subclass. $result = $db->query ("SELECT * from class where f_id=1"); Find the f_id=1 category, which is to find the ' news ' subclass. while ($row = $result->fetch_assoc ()) { echo $row [' name ']. " "; This will be the ' news ' of the sub-class loop out. Note: Just subclasses, excluding Sun Tzu classes. } //write here, we will find a problem, if this classification is 10 levels of classification, do we want to write 10 loops to each of its subclasses loop out? If it is more level classification, this writing is obviously unrealistic. What is the solution? We can write a recursive function, pass the f_id as a parameter, and loop through each of the f_id values, that is, to loop through the subclasses of each f_id value. //First we save the values of each category in a two-dimensional array, which is useful in the recursive function below. $result = $db->query ("SeleCT * from class '); while ($row = $result->fetch_assoc ()) { $arr []=array ($row [id], $row [f_id], $row [name]); Each row holds a category of id,f_id,name information. } function Fenlei ($f _id=0) {//$f _id initialized to 0, that is, Cycle . global $arr from the maximum classification;//Declaration $arr is a global variable that can be referenced in a function. for ($i =0 $i < count ($arr), $i + +) {//loops for each category. if ($arr [$i][1]== $f _id) {//$arr [$i][1] represents the f_id value of the $I+1 classification. Start $f_id=0, that is, the output of the f_id=0 classification. Echo $arr [$i][2]. " < br> "; $arr [$i][1] represents the value of the name of the $i+1 category. Fenlei ($arr [$i][0]); $arr [$i][1] represents the value of the ID of the $i+1 category. Recursion, which is to recycle your own subclass as a f_id parameter. } } } fenlei (); Use this function .
Advantages of this classification method:
1, simple database structure, only CID ParentID name three fields, no redundant fields
2, do not use recursive query, all operations need only one SQL statement
3, all data after reading a database, in the array analysis processing, save database server resources