Unlimited classification, but the efficiency is not the best... Do you have code that is faster and more efficient?
Recursion will affect efficiency when there are many categories!
Please share it!
Class. SQL
---- TABLE structure 'class' -- set names utf8; create table 'class' ('id' int (10) not null auto_increment, 'name' varchar (250) character set utf8 default NULL, 'classid' int (10) default NULL, 'Port' int (10) not null default '0', primary key ('id ')) ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 11; ---- export the table data 'class' -- insert into 'class' ('id', 'name', 'classid ', 'sort ') VALUES (1, 'China', 0, 1), (2, 'guangxi', 1, 1), (3, 'guilin ', 2, 2), (4, 'guangdong ', 1, 2), (5, 'beijing', 1, 3), (6, 'dongguan', 4, 10 ), (7, 'nanning ', 2, 10), (8, 'yangshuo', 3, 10), (9, 'liuzhou ', 2, 10), (10, 'guangzhou ', 4, 10 );
Index. php
Unlimited ClassificationAdd category in the category list
Select_db ('test'); switch ($ _ GET ['action']) {case 'add': $ class_arr = array (); $ SQL = "select * from 'class' order by sort asc, id Desc"; $ query = $ mysql-> query ($ SQL ); while ($ row = $ mysql-> fetch_array ($ query) {$ class_arr [] = array ($ row ['id'], $ row ['name'], $ row ['classid '], $ row ['sort']);}?>
Query ($ SQL); msg ('added successfully! ', 'HTTP: // blog.csdn.net/phpfenghuo/article/details/18733323? Action = '); break; case 'edit': $ class_arr = array (); $ SQL = "select * from 'class' order by sort asc, id Desc "; $ query = $ mysql-> query ($ SQL); while ($ row = $ mysql-> fetch_array ($ query )) {$ class_arr [] = array ($ row ['id'], $ row ['name'], $ row ['classid '], $ row ['sort ']);} $ SQL = "select * from 'class' where id = ". $ _ GET ['id']; $ query = $ mysql-> query ($ SQL); $ row = $ mysql-> fetch_array ($ query); if ($ row) {?>
Query ($ SQL); $ row = $ mysql-> fetch_array ($ query); if ($ row) {if ($ row ['id'] ==$ _ POST ['classid ']) {msg ('modification failed, you cannot use your own subcategory! ', 'HTTP: // blog.csdn.net/phpfenghuo/article/details/18733323? Action = ');} else {$ SQL = "update 'class' set 'name' = '". $ _ POST ['name']. "', 'classid' = ". $ _ POST ['classid ']; $ SQL. = ", 'sort '= ". $ _ POST ['sort ']. "where 'id' = ". $ _ POST ['id']; $ mysql-> query ($ SQL); msg ('modified successfully! ', 'HTTP: // blog.csdn.net/phpfenghuo/article/details/18733323? Action = ') ;}} break; case 'del': $ SQL = "select * from 'class' where id = ". $ _ GET ['id']; $ query = $ mysql-> query ($ SQL); $ row = $ mysql-> fetch_array ($ query); if ($ row) {$ mysql-> query ("delete from 'class' where id = ". $ _ GET ['id']); msg ('deleted successfully! ', 'HTTP: // blog.csdn.net/phpfenghuo/article/details/18733323? Action = ');} else {msg (' the record does not exist! ', 'HTTP: // blog.csdn.net/phpfenghuo/article/details/18733323? Action = ');} break; case '': $ class_arr = array (); $ SQL =" select * from 'class' order by sort asc, id Desc "; $ query = $ mysql-> query ($ SQL); while ($ row = $ mysql-> fetch_array ($ query )) {$ class_arr [] = array ($ row ['id'], $ row ['name'], $ row ['classid '], $ row ['sort ']);}?>
Category name |
Sort |
Operation |
\ N "; echo"". $ N." | ---- ". $ class_arr [$ I] [1]."\ N "; echo"". $ Class_arr [$ I] [3]."\ N "; echo"Modify "; echo" delete "; echo"\ N "; echo"\ N "; infinite_arr ($ m + 1, $ class_arr [$ I] [0]) ;}} function infinite_select ($ m, $ id, $ index) {global $ class_arr; $ n = str_pad ('', $ m, '-', STR_PAD_RIGHT); $ n = str_replace ("-"," ", $ n ); for ($ I = 0; $ I
". $ N." | ---- ". $ class_arr [$ I] [1]."\ N ";}else {echo"
". $ N." | ---- ". $ class_arr [$ I] [1]."\ N ";}infinite_select ($ m + 1, $ class_arr [$ I] [0], $ index );}}} /*** ----------------------------- database operation class ----------------------------- **/class mySql_Class {function _ construct ($ host, $ user, $ pass) {@ mysql_connect ($ host, $ user, $ pass) or die ("database connection failed! "); Mysql_query (" set names 'utf8' ");} function select_db ($ db) // connection table {return @ mysql_select_db ($ db);} function query ($ SQL) // execute the SQL statement {return @ mysql_query ($ SQL);} function fetch_array ($ fetch_array) {return @ mysql_fetch_array ($ fetch_array, MYSQL_ASSOC);} function close () // close the database {return @ mysql_close ();} function insert ($ table, $ arr) // Add record {$ SQL = $ this-> query ("INSERT INTO '$ table '('". implode ('','', array_keys ( $ Arr). "') VALUES ('". implode ("','", $ arr). "')"); return $ SQL ;}}?>