Unlimited classification of PHP + Mysql-PHP source code

Source: Internet
Author: User
PHP + Mysql unlimited classification a PHP project uses classification, but it is not confirmed that level 4 still needs to use Level 5, just think about creating an infinitely classification.
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 new field is added to the Mysql table. the current database is as follows:
Table name w_faqclass: id: Auto-increment | pid: parent class ID | xid: sorting ID | classname: category name | rank: Grade
Definition:
Level 1 classification, pid is 0, rank is "/"
Level 2 classification, pid is the id of level 1 classification, rank is "/Level 1 classification id /"
Level 3 classification, pid is the id of level 2 classification, rank is "/Level 1 classification id/level 2 classification id /"
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 category

 "; 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

 "; 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. delete and query

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

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.