Two solutions for php Infinitus classification implementation

Source: Internet
Author: User
This article introduces two solutions for the implementation of Infinitus classification in php. If you need a friend, I wrote an Infinitus classification today and pasted the code below.

Method of classify one
The code is as follows:
/*

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 ('failed to add '); 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 ('failed to add '); script ";
}
}
} Elseif ($ act = "add "){
?>

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












While ($ arr = mysql_fetch_assoc ($ res) {?> }?>
Id Category name Path Operation
"> Edit |
"> Delete


} Elseif ($ act = "edit "){
$ Id = $ _ GET ['type _ id'];
$ SQL = "select * from 'types' where type_id = $ id ";
$ Res = mysql_query ($ SQL );
Echo "";
} 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
The code is as follows:
/*

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

} 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 ('failed to add '); 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 );
?>












While ($ arr = mysql_fetch_assoc ($ res) {?> }?>
Id Category name Path Operation
"> Edit |
"> Delete


} 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.

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.