PHP code: // unlimited classification-path // principle: splits the path to obtain all desired PIDs and IDs. The path is a field in the design table, contains a series of IDs from the ancestor id to the parent id // this method, whether viewed or deleted, etc... syntaxHighlighter. all (); PHP code: // unlimited classification-path used // principle: all desired PIDs and IDs are obtained through Path splitting, A path is a field when designing a table. it contains a series of IDs from the ancestor id to the parent id. // this method, whether viewed or deleted, and other operations, you only need to execute an SQL statement once to achieve the desired results. // This method is also preferred, because my principle is that recursion is not used, instead of recursion $ link = mysql_connect ('localhost', 'username, 'password'); if (mysql_errno () {echo 'database connection failed :'. mysql_error ();} mysql_select_db ('Db _ kind'); mysql_set_charset ('utf8'); // concat (), concatenate a string into a new string // the following code completes an infinite classification reality $ SQL = "select concat (path, '-', id) as conpath, id, name, path from wx_kind order by conpath "; $ result = mysql_query ($ SQL); while ($ row = mysql_fetch_assoc ($ result) {$ arr = array (); // you can select the path field or the new conpath field to split the string. // note one point for The explode () function, no matter whether the string to be split contains any character that uses the parameter, at least one array containing an element $ count = count (explode ('-', $ row ['path']) is returned. -1; // str_repeat (), repeat the string mainly for the sake of clear format $ str = str_repeat ('', $ count); echo $ str. $ id. '=> '. $ row ['name'].' ';} /*********************************** Delete operation, the same path is used. for example, if you want to delete the Thunder classification, first find the path ************************ * ************* // * $ sql1 = "select id, path from wx_kind where id = 20 "; $ result1 = mysql_query ($ sql1); $ row = mysql_fetch_assoc ($ result1); $ id = $ row ['id']; $ path = $ row ['path']; // Construct all paths with this category as the parent class or ancestor class $ new_path = $ path. '-'. $ id; // you can delete $ sql2 = "delete from wx_kind where id = {$ id} or path like '{$ New_path} %' "; $ result2 = mysql_query ($ sql2); if ($ result2 & mysql_affected_rows () {echo 'deleted successfully! ';} Else {echo' deletion failed! ';} *//************************************* *************** // Use recursive functions, I don't explain that much. let's look at the code directly. The main difference is that function display_classify ($ pid = 0, $ num = 0) {$ SQL = "select id, name from wx_kind where pid = {$ pid} "; $ result = mysql_query ($ SQL); while ($ row = mysql_fetch_assoc ($ result )) {$ id = $ row ['id']; $ str = str_repeat ('', $ num); echo $ str. $ id. '=> '. $ row ['name'].' '; Display_classify ($ id, $ num + 1, $ sid) ;}// display_classify (); /*************************************** * ************* // The deletion method is as follows, let's take a look at the sequence of deletion as described below. and where to delete the del_classify function ($ id) {// use recursion to find all sub-categories whose id is parent id or ancestor id, and then delete them from the inside out, note the order of deletion: $ SQL = "select id, name from wx_kind where pid ={$ id}"; $ result = mysql_query ($ SQL ); while ($ row = mysql_fetch_assoc ($ result) {$ id = $ row ['id']; del_classify ($ id );} // Delete $ sql1 = "delete from wx_kind where id = {$ id}" outside the loop, instead of any prompt $ result = mysql_query ($ sql1); if (! ($ Result & mysql_affected_rows () {$ bool = false;} else {$ bool = true;} return $ bool;} // del_classify (5 ); the following is the database code -- database: 'db _ kind' -- tables -- TABLE structure 'wx _ kind_dump '-- create table 'wx _ kind_dump' ('id' int (11) not null auto_increment, 'pid 'int (11) not null, 'name' char (40) not null, 'path' char (40) not null, primary key ('id ')) ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 26; -- export the table data 'wx _ kind_dump '-- insert into 'wx _ kind_dump' VALUES (1, 0, 'news ', '0'); insert into 'wx _ kind_dump 'values (2, 0, 'video', '0'); insert into 'wx _ kind_dump 'VALUES (3, 0, 'Image', '0'); insert into 'wx _ kind_dump 'values (4, 0, 'reading', '0 '); insert into 'wx _ kind_dump 'VALUES (5, 1, 'political News', '0-1'); insert into 'wx _ kind_dump 'VALUES (6, 1, 'Financial News ', '0-1'); insert into 'wx _ kind_dump 'values (7, 1, 'Entertainment News', '0-1 '); insert into 'wx _ kind_dump 'VALUES (8, 1, 'Sports News', '0-1'); insert into 'wx _ kind_dump 'VALUES (9, 8, 'Basketball ', '0-1-8'); insert into 'wx _ kind_dump 'values (10, 8, 'soccer ', '0-1-8 '); insert into 'wx _ kind_dump 'values (11, 8, 'F1', '0-1-8 '); insert into 'wx _ kind_dump' VALUES (12, 8, 'tennis tennis ', '0-1-8'); insert into 'wx _ kind_dump 'values (13, 9, 'International basketball ', '0-1-8-9 '); insert into 'wx _ kind_dump 'values (14, 9, 'CBA', '0-1-8-9 '); INSERT INTO 'wx _ kind_dump' VALUES (15, 9, 'Cuba ', '0-1-8-9'); insert into 'wx _ kind_dump 'values (16, 9, 'NBA ', '0-1-8-9 '); insert into 'wx _ kind_dump 'values (17, 9, 'NCAA ', '0-1-8-9'); insert into 'wx _ kind_dump 'VALUES (18, 16, 'Hot heat ', '0-1-8-9-16'); insert into 'wx _ kind_dump 'values (19, 16, 'Lakers ', '0-1-8-9-16 '); insert into 'wx _ kind_dump 'values (20, 16, 'Thunder', '0-1-8-9-16 '); insert into 'wx _ kind_dump' VALUES (21, 16, 'Celt', '0-1-8-9-16 '); insert into 'wx _ kind_dump 'values (22, 18, 'James', '0-1-8-9-16-18 '); insert into 'wx _ kind_dump 'values (23, 18, 'Wade ', '0-1-8-9-16-18'); insert into 'wx _ kind_dump 'VALUES (24, 20, 'kd ', '0-1-8-9-16-20'); insert into 'wx _ kind_dump 'values (25, 20, 'Ws ', '0-1-8-9-16-20 ');
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