Php + mysql implement unlimited classification, phpmysql unlimited _ PHP Tutorial

Source: Internet
Author: User
Php + mysql supports unlimited classification, while phpmysql supports unlimited classification. Php + mysql implements unlimited classification. phpmysql unlimited project idea analysis: a PHP project requires classification, but it is not sure how many levels are divided. Therefore, it is intended to be an infinitely classified project. At the beginning, I thought php + mysql would implement unlimited classification, and phpmysql would be infinitely classified.

Analysis of project ideas:A php project requires classification, but it is not sure how many levels are divided. Therefore, it is intended to be an infinitely classified project.
In the beginning, we thought that the database had four values as before, as shown below:
Id: Auto-increment | pid: parent class ID | xid: sort ID | classname: category name
Later I thought it was inconvenient to read and modify the data, and it was especially inconvenient to read the product. I changed it to the following solution:
A field is added to the Mysql table.DatabaseAs follows:
Table name w_faqclass: id: Auto-increment | pid: parent class ID | xid: sorting ID | classname: category name | rank: Grade
Definition:
Level 1, Pid is 0, rank is "/"
Level 2,Pid is the id of the first-level classification, and rank is "/id of the first-level classification /"
Level 3 classification,Pid is the id of the second-level classification, rank is "/id of the first-level classification/id of the second-level classification /"
So on...
1. basic functions

/* Recursively return the list of infinitely sorted arrays that have been sorted. if you don't want to use recursion, you can use like to retrieve the array and then sort it. I'm too lazy and don't write the way to get it, in fact, it is better to use like. we recommend that you use $ datatable: Data table name $ startid: start parent class ID $ wheretColumns: parent class column name $ xColumns: sorting column name $ xtype: sorting method $ returnArr: returns the array */function ReadClass ($ datatable, $ startid, $ xtype, $ returnArr) {$ db = $ datatable; $ sid = $ startid; $ xtype = $ xtype; $ lu = $ returnArr; $ SQL = "select * from '". $ db. "'where' pid '= '". $ sid. "'Order by xid ". $ xtype. ";"; $ cresult = mysql_query ($ SQL); if (mysql_num_rows ($ cresult)> 0) {while ($ rs = mysql_fetch_array ($ cresult )) {$ lunum = count ($ lu); $ lu [$ lunum] ['id'] = $ rs ['id']; $ lu [$ lunum] ['pid '] = $ rs ['pid']; $ lu [$ lunum] ['rank '] = $ rs ['rank']; $ lu [$ lunum] ['classname'] = $ rs ['classname']; $ lu [$ lunum] ['xid'] = $ rs ['xid']; $ lu = ReadClass ($ db, $ rs ['id'], $ xtype, $ lu) ;}return $ lu ;}/ * query a value in a table, only one value $ able: Data table name $ wherevalue: condition value $ selectColumns: query column name $ whereColumns: condition column */function SelectValue ($ datatable, $ wherevalue, $ selectColumns, $ whereColumns) {$ SQL = "select '". $ selectColumns. "'from '". $ datatable. "'where '". $ whereColumns. "'= '". $ wherevalue. "';"; $ result = mysql_query ($ SQL); while ($ rs = mysql_fetch_array ($ result) {return $ rs [$ selectColumns];}

2. add a category (select is directly used for selection)

<? Php $ classArr = ReadClass ('w _ faqclass', '0', 'asc ', array (); $ canum = count ($ classArr); echo""; Echo"Primary category"; For ($ I = 0; $ I <$ canum; $ I ++) {$ rankArr = split ("/", $ classArr [$ I] ['rank ']); $ ranknum = count ($ rankArr); $ t = ""; for ($ j = 1; $ j <$ ranknum; $ j ++) {// format the display subclass $ t. = "too many requests";} echo"". $ T. $ classArr [$ I] ['classname']."";} Echo""?> // For the operation during storage, you need to determine whether the primary category is used. when the primary class is used, the rank value is set to // query the rank value of the parent class, add the id value of the parent class with the rank of the parent class if ($ pid! = 0) {$ pidrank = SelectValue ('w _ faqclass', $ pid, 'rank ', 'id'); $ rank = $ pidrank. $ pid. "/";} else {$ rank = "/";}

3. modify a category

<? Php/* Note: Because it is a modification, all values of the current category have been read during loading on this page, corresponding to: $ pid, $ rank */$ classArr = ReadClass ('w _ faqclass', '0', 'asc ', array (); $ canum = count ($ classArr); echo""; Echo"Primary category"; For ($ I = 0; $ I <$ canum; $ I ++) {// because it is modified, you cannot select the current category or the following category, advantages of adding more rank values, haha, previously, when making a single pid value, we had to use the next recursive query while ($ ids = $ classArr [$ I] ['id'] | strstr ($ classArr [$ I] ['rank '], $ rank. $ ids. "/") {$ I ++;} $ rankArr = split ("/", $ classArr [$ I] ['rank ']); $ ranknum = count ($ rankArr); $ t = ""; for ($ j = 1; $ j <$ ranknum; $ j ++) {$ t. = "too many variables";} if ($ pid = $ classArr [$ I] ['id']) {$ selected = "selected ";} else {$ selected = "";} echo"". $ T. $ classArr [$ I] ['classname']."";} Echo""?> // Operation during storage // The rank value of all sub-categories of this category needs to be changed when the change is made. select the rank value that is common to the original sub-category, that is, the rank value of the classification plus its ID value // REPLACE if ($ pid! = 0) {$ pidrank = SelectValue ('w _ faqclass', $ pid, 'rank ', 'id'); $ rank = $ pidrank. $ pid. "/" ;}else {$ rank = "/" ;}$ orank = SelectValue ('w _ faqclass', $ ids, 'rank ', 'id '). $ ids. "/"; $ nrank = $ rank. $ ids. "/"; mysql_query ("update' w _ faqclass 'SET rank = REPLACE (rank ,'". $ orank. "','". $ nrank. "');"); mysql_query ("update' w _ faqclass 'set' classname' = '". $ classname. "', 'xid' = '". $ xid. "', 'pid' = '". $ pid. "', 'rank' = '". $ rank. "'where' ID' = '". $ ids. "';");

4. deletion and query are simple. I will not repeat it here. as mentioned, remember to confirm whether a subclass exists under this class before deletion.

$zid = SelectValue('w_faqclass',$ids,'id','pid'); if($zid>0){  ...}

The above describes how to implement Infinitus classification in php + mysql. I hope it will be helpful for your learning.

Idea analysis of the Alipay project: a PHP project requires classification, but is not sure how many levels are divided, so it is intended to be an infinitely classified project. At first, I thought it was...

Related Article

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.