Php + mysql for unlimited Classification

Source: Internet
Author: User
This article mainly introduces php + mysql to implement unlimited classification. a php project needs to use classification and create a php infinitus classification. For more information, see

This article mainly introduces php + mysql to implement unlimited classification. a php project needs to use classification and create a php infinitus classification. For more information, see

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 "http://www.jb51.net /"
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 (" http://www.jb51.net /", $ 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. "http://www.jb51.net/";} else {$ rank = "http://www.jb51.net /";}

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. "http://www.jb51.net/") {$ I ++;} $ rankArr = split ("http://www.jb51.net/", $ 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. "http://www.jb51.net/";} else {$ rank = "http://www.jb51.net/";} $ orank = SelectValue ('W _ faqclass', $ ids, 'rank ', 'id '). $ ids. "http://www.jb51.net/"; $ nrank = $ rank. $ ids. "http://www.jb51.net/"; 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.

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.