Unlimited classification-summary _ PHP Tutorial

Source: Internet
Author: User
Unlimited category-summary. PHP code part: unlimited classification-path principle: all desired PIDs and IDs are obtained through Path splitting. The path is a field during table design, contains the PHP code part from the ancestor:

// Unlimited classification-path used

// Principle: all desired PIDs and IDs are obtained through Path splitting. The path is a field in the design table and contains a series of IDs from the ancestor id to the parent id.

// This method can achieve the desired effect by executing an SQL statement once, regardless of viewing or deleting other operations.

// This method is also my favorite one, because my principle is that we can never use recursion without 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 (), concatenates 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 That The explode () function returns an array containing at least one element no matter whether the string to be split contains characters using parameters.

$ Count = count (explode ('-', $ row ['path'])-1;

// Str_repeat (), repeat the string mainly for clear format

$ Str = str_repeat ('', $ count );

Echo $ str. $ id. '=>'. $ row ['name'].'

';

}

/***********************************

If you want to delete a file, use the same path.

For example, you want to delete the Thunder classification.

First, find the path based on its id

**************************************/

/* $ 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 that use this category as the parent class or ancestor class

$ New_path = $ path. '-'. $ id;

// The deletion operation can be performed below.

$ Sql2 = "delete from wx_kind where id = {$ id} or path like '{$ new_path} % '";

$ Result2 = mysql_query ($ sql2 );

If ($ result2 & mysql_affected_rows ()){

Echo 'deleted! ';

} Else {

Echo 'deletion failed! ';

}*/

/*************************************** **************/

// The method for using recursive functions is not explained as much. let's look at the code directly, mainly because the searching method is different.

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 following is the deletion method. Take a look at the order of the deletion as described below, and pay attention to where to delete the deletion.

Function del_classify ($ id ){

// Use recursion to find all sub-categories whose id is parent id or ancestor id, and then delete them from the inside out to the outside. pay attention to 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 );

}

// The delete operation is performed out of the loop.

$ Sql1 = "delete from wx_kind where id = {$ id }";

// Delete the file directly, instead of any prompts.

$ 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'

--

----------------------------------------------------------

--

-- 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', '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 ');

Future // unlimited classification-path // principle: all desired PIDs and IDs are obtained through Path splitting. The path is a field in table design, contains an ancestor from...

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.