Today, I wrote an infinitus classification and I pasted the code below.
Method of classify one
Copy codeThe Code is as follows:
<? Php
/*
Reader: This is a self-written infinitus classification implementation method. The editing method only edits the category name.
I don't have the ability to perform any mobile operations. I'm sorry.
Method 1: create table 'types '(
'Type _ id' int (11) not null AUTO_INCREMENT,
'Type _ name' varchar (20) not null,
'Type _ p_id 'varchar (64) not null default '-',
Primary key ('Type _ id '),
KEY 'Type _ name' ('Type _ name '),
KEY 'tname' ('Type _ name ')
) ENGINE = MyISAM AUTO_INCREMENT = 14 default charset = utf8
Note:
Field validation is not performed here;
Type_id indicates auto-increment of primary key
Type_name indicates the category name.
Type_p_id indicates the category path. The category path here is the category path of the upper parent class, and the primary key IDs of this class are separated by commas.
*/
Error_reporting (7 );
Header ("Content: text/html; charset = UTF-8 ");
// Perform operations first
$ Arr = array ('LIST', 'add', 'del ', 'edit', 'addok', 'edit _ OK ');
$ Act = in_array ($ _ GET ['ac'], $ arr )? $ _ GET ['ac']: $ arr [0];
// Connect to the database
$ Conn = mysql_connect ("localhost", "root", "root") or die ('database connection failed ');
Mysql_select_db ("study", $ conn );
Mysql_query ("set names utf8 ");
// Sort by category Layer
$ SQL = "select * from types order by type_p_id ";
$ Sql1 = mysql_query ($ SQL );
// Add a category
If ($ act = "addok "){
$ Type_id = $ _ POST ['Type _ id'];
$ Type_name = $ _ POST ['Type _ name'];
// If it is equal to 0, it indicates that it is a directory
If ($ type_id = "0 "){
$ SQL = "insert into types set type_name = '{$ type_name }'";
$ Res = mysql_query ($ SQL );
$ Id = mysql_insert_id ();
$ SQL = "update types set type_p_id = '$ id, 'where type_id = $ id ";
$ Res = mysql_query ($ SQL );
If (mysql_affected_rows ()> 0 ){
Echo "<script> location. href = 'OK. php? Act = list' </script> ";
} Else {
Echo "<script> alert ('add failed'); </script> ";
}
} // If not equal to 0
Else {
// Locate the type_p_id under the id based on the typeid
$ SQL = "select type_p_id from 'types' where type_id = $ type_id ";
$ Res = mysql_query ($ SQL );
$ Res = mysql_fetch_assoc ($ res );
$ Type_id = $ res ['Type _ p_id '];
// Insert the name first
$ SQL = "insert into types set type_name = '{$ type_name }'";
$ Res = mysql_query ($ SQL );
// Obtain the last executed id and then update the data. The main update is type_p_id.
$ Id = mysql_insert_id ();
$ SQL = "update types set type_p_id = '$ type_id $ id, 'where type_id = $ id ";
$ Res = mysql_query ($ SQL );
If ($ res ){
Echo "<script> location. href = 'OK. php? Act = list' </script> ";
} Else {
Echo "<script> alert ('add failed'); </script> ";
}
}
} Elseif ($ act = "add "){
?>
<Form method = "post" action = "? Ac = addok ">
New category name: <input type = "text" name = "type_name"> <br/>
Current category: <select name = "type_id">
<Option value = "0"> top-level classification </option>
<? Php
// Cyclically traverse the category
While ($ res = mysql_fetch_assoc ($ sql1 )){
// Find "," Number of times
$ M = substr_count ($ res ['Type _ p_id '], ",");
// Replace "space" with spaces"
$ Strpad = str_pad ("", $ m * 8*2 ,"");
If ($ m = 1 ){
$ Sele = "disabled ";
} Else {
$ Sele = "";
}
?>
<Option value = "<? Php echo $ res ['Type _ id']?> "> <? Php echo $ strpad. $ res ['Type _ name']?> </Option>
<? Php
}
?>
</Select> <br/>
<Input type = "submit" value = "add"/>
</Form>
<? Php
} Elseif ($ act = "list "){
// Obtain the list and sort it by category Layer
$ SQL = "select * from types order by type_p_id ";
$ Res = mysql_query ($ SQL );
?>
<Table width = "50%" style = "font-size: 12px; margin-left: 20%;">
<Tr> <td> id </td> <td> category name </td> <td> path </td> <td> operation </td> </tr>
<? Php
While ($ arr = mysql_fetch_assoc ($ res) {?>
<Tr>
<Td> <? Php echo $ arr ['Type _ id']?> </Td>
<Td> <? Php echo $ arr ['Type _ name']?> </Td>
<Td> <? Php echo $ arr ['Type _ p_id ']?> </Td>
<Td> <a href = "? Ac = edit & type_id = <? Php echo $ arr ['Type _ id'];?> "> Edit </a> |
<A href = "? Ac = del & type_id = <? Php echo $ arr ['Type _ id'];?> "> Delete </a> </td>
</Tr>
<? Php
}
?>
</Table>
<Input type = "button" value = "add" onclick = "(location. href = '? Ac = add') ">
<? Php
} Elseif ($ act = "edit "){
$ Id = $ _ GET ['Type _ id'];
$ SQL = "select * from 'types' where type_id = $ id ";
$ Res = mysql_query ($ SQL );
Echo "<form method = 'post' action = '? Ac = edit_ OK '> ";
While ($ arr = mysql_fetch_assoc ($ res )){
Echo "current name: {$ arr ['Type _ name']}". "<br/> ";
Echo "new name: <input type = 'text' name = 'n' _ type_name '>". "<br/> ";
Echo "<input type = 'den den 'value = {$ arr ['Type _ id']} name = 'id'/> ";
}
Echo "<input type = 'submit 'value = 'update'> ";
Echo "</form> ";
} Elseif ($ act = "edit_ OK "){
$ Name = trim ($ _ POST ['n' _ type_name ']);
$ Id = $ _ POST ['id'];
If (! Empty ($ name )){
$ SQL = "update 'types' set type_name = '$ name' where type_id = $ id ";
Mysql_query ($ SQL );
Echo "<script> location. href ('OK. php? Act = list') </script> ";
} Else {
Echo "<script> location. href ('OK. php? Act = list') </script> ";
}
} Elseif ($ act = 'del '){
// Here, the deletion is to delete both the current category and the current subcategory, so $ id % is used.
$ Id = $ _ GET ['Type _ id'];
$ SQL = "delete from 'types' where type_p_id like '$ id % '";
$ Res = mysql_query ($ SQL );
If ($ res ){
Echo "<script> location. href ('OK. php? Act = list') </script> ";
} Else {
Echo "<script> alert ('deletion failed'); </script> ";
}
}
?>
Types table:
Below is:
Method of classify two
Copy codeThe Code is as follows:
<? Php
/*
Reader:
This is a simple way to edit the infinitus classification implementation method (You know .) No more
I don't have the ability to perform any mobile operations. I'm sorry.
Method 2:
Create table 'types '(
'Type _ id' int (11) not null AUTO_INCREMENT,
'Type _ name' varchar (20) not null,
'Type _ p_id 'varchar (64) not null,
Primary key ('Type _ id '),
KEY 'Type _ name' ('Type _ name '),
KEY 'tname' ('Type _ name ')
) ENGINE = MyISAM AUTO_INCREMENT = 14 default charset = utf8
Note:
Field validation is not performed here;
Type_id indicates auto-increment of primary key
Type_name indicates the category name.
Type_p_id indicates the category path. The category path here is the upper-level category id. If it is the current category, the value is 0.
*/
Error_reporting (7 );
Header ("Content-type: text/html; charset = UTF-8 ");
// Perform operations first
$ Arr = array ('LIST', 'add', 'del ', 'edit', 'addok', 'edit _ OK ');
$ Act = in_array ($ _ GET ['ac'], $ arr )? $ _ GET ['ac']: $ arr [0];
// Connect to the database
$ Conn = mysql_connect ("localhost", "root", "root") or die ('database connection failed ');
Mysql_select_db ("study", $ conn );
Mysql_query ("set names utf8 ");
If ($ act = "add "){
/**
* @ Access public
* @ Param array $ the array in the types array must be referenced &
* @ Param interal $ upper-level category id of the pid
* @ Param int $ path the category layer defaults to 0 and increases by one for each loop
* @ Return array ();
*/
Function getTypes (& $ types = array (), $ pid = 0, $ path = 0 ){
$ SQL = "select * from 'type' where type_p_id = $ pid ";
$ Res = mysql_query ($ SQL );
While ($ row = mysql_fetch_assoc ($ res )){
$ Str = "";
For ($ I = 0; $ I <$ path; $ I ++ ){
$ Str. = "";
}
$ Row ['new _ type_name '] = $ str. $ row ['Type _ name'];
$ Types [] = $ row;
GetTypes ($ types, $ row ['Type _ id'], ($ path + 1 ));
}
Return $ types;
}
// Obtain the class call Function
GetTypes ($ types );
// Obtain the list and sort it by category Layer
$ Sql1 = "select * from type order by type_id ";
$ Sqll = mysql_query ($ sql1 );
?>
<Form method = "post" action = "? Ac = addok ">
New category name: <input type = "text" name = "type_name"> <br/>
Current category: <select name = "type_id">
<Option value = "0"> top-level classification </option>
<? Php
// Loop this array to output the correct classification
For ($ I = 0; $ I <count ($ types); $ I ++ ){
?>
<Option value = "<? Php echo $ types [$ I] ['Type _ id']?> "> <? Php echo $ types [$ I] ['new _ type_name ']?> </Option>
<? Php
}
?>
</Select> <br/>
<Input type = "submit" value = "add"/>
</Form>
<? Php
} Elseif ($ act = "addok "){
$ Type_name = $ _ POST ['Type _ name'];
$ Type_id = $ _ POST ['Type _ id'];
$ SQL = "insert into 'type' (type_name, type_p_id) values ('$ type_name', '$ type_id ')";
$ Res = mysql_query ($ SQL );
If (mysql_insert_id ()> 0 ){
Echo "<script> location. href = 'Two. php? Act = list' </script> ";
} Else {
Echo "<script> alert ('add failed'); </script> ";
}
} Elseif ($ act = "list "){
// Obtain the list and sort it by category Layer
$ SQL = "select * from type order by concat (type_id, type_p_id )";
$ Res = mysql_query ($ SQL );
?>
<Table width = "50%" style = "font-size: 12px; margin-left: 20%;">
<Tr> <td> id </td> <td> category name </td> <td> path </td> <td> operation </td> </tr>
<? Php
While ($ arr = mysql_fetch_assoc ($ res) {?>
<Tr>
<Td> <? Php echo $ arr ['Type _ id']?> </Td>
<Td> <? Php echo $ arr ['Type _ name']?> </Td>
<Td> <? Php echo $ arr ['Type _ p_id ']?> </Td>
<Td> <a href = "? Ac = edit & type_id = <? Php echo $ arr ['Type _ id'];?> "> Edit </a> |
<A href = "? Ac = del & type_id = <? Php echo $ arr ['Type _ id'];?> "> Delete </a> </td>
</Tr>
<? Php
}
?>
</Table>
<Input type = "button" value = "add" onclick = "(location. href = '? Ac = add') ">
<? Php
} Elseif ($ act = "del "){
/***
To delete a category, you must delete its subcategory.
For more information about how to start mysql transactions, see
*/
Mysql_query ("set autocommit = 1"); // start the transaction
$ Type_id = $ _ GET ['Type _ id'];
// Delete this category
$ Sqlone = "delete from 'type' where type_id = $ type_id ";
// Delete the subcategory under this category
$ Sqltwo = "delete from 'type' where type_p_id = $ type_id ";
$ Res1 = mysql_query ($ sqlone );
$ Res2 = mysql_query ($ sqltwo );
If ($ res1 & $ res2)
{
Mysql_query ("COMMIT ");
Echo "<script> location. href = 'Two. php? Act = list' </script> ";
} Else {
Mysql_query ("ROLLBACK ");
Echo "<script> alert ('deletion failed'); </script> ";
}
}
?>
Type table:
Below is
I hope you will forgive me for not writing well.