Php+mysql Infinite class classification, Phpmysql infinite level _php tutorial

Source: Internet
Author: User

PHP+MYSQL enables unlimited class classification, Phpmysql infinite level


Project Thinking Analysis: a PHP project to use the classification, but not a certain number of levels, so you want to make unlimited classification.
At first, I thought, as before, the database built 4 values, as follows:
ID: auto-increment | PID: Parent Class ID | XID: Sort ID | ClassName: Category Name
Later thought of this when reading data and modification is not convenient, and in the product is particularly inconvenient to read, at the time changed to the following scheme:
A new field is added to the MySQL table, and now the database is as follows:
Table name W_faqclass:id: self-increment | PID: Parent Class ID | XID: Sort ID | ClassName: Category name | Rank: Rank
Defined:
First Class classification , PID 0, rank "/"
Class Two classification,PID for the first class classification of the Id,rank for "/first-class classification ID"
Class Three classification,PID two class Id,rank for "/first class classification of ID/two class ID"
etc...
1. Basic Functions

/* Facilitates recursive return of an array of infinite classes that have already been sorted do not want to use the recursive Word can also be used like to get after the order, I am lazy, do not write the kind of access, in fact, with like better, recommended in that way $datatable: Data table name $startid: Start Parent class Id$wheretcolumns: Parent class Column name $xcolumns: Sort column name $xtype: Sort by $returnarr: Returns an 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, return only one value $datatable: Data table name $wherevalue: Condition value $selectcolumns: Query column name $wherecolumns: Conditional 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 classification (directly to select for selection)

<?php  $classArr = ReadClass (' w_faqclass ', ' 0 ', ' ASC ', Array ());  $canum = count ($CLASSARR);     echo "";  echo "Main category ";  for ($i =0; $i < $canum; $i + +) {    $RANKARR = split ("/", $CLASSARR [$i] [' rank ']);    $ranknum = count ($RANKARR);    $t = "";    for ($j =1; $j < $ranknum; $j + +) {//For formatting display sub      -class $t. = "├┄┄";    }    echo "". $t. $CLASSARR [$i] [' classname '].";  }  echo ""?>  //save operation, you need to determine whether the primary classification, when the main class, the rank value is set to///query the rank value of the parent class, with the parent class rank plus the parent class ID value if ($pid! = 0) {  $pidrank = Select Value (' W_faqclass ', $pid, ' rank ', ' id ');   $rank = $pidrank. $pid. " /";} else{  $rank = "/";  }

3. Modify the classification

<?php/* Note that because it is a modification, all values of the current classification are read out when this page is loaded, corresponding to: $pid, $rank */$classArr = ReadClass (' w_faqclass ', ' 0 ', ' ASC ', Array ());  $canum = count ($CLASSARR); echo " main category "; for ($i =0; $i < $canum; $i + +) {//Because it is modified, so the current classification can not choose itself or the following categories, add a rank value of the advantages Ah, haha, used to do a single PID value when the time here also have to use a 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. = "├┄┄";      } if ($pid = = $CLASSARR [$i] [' ID ']) {$selected = "selected";    }else{$selected = "";  } echo "". $t. $CLASSARR [$i] [' classname ']. ""; } echo ""?>//Save action//To make changes to the classification of all sub-classification of the rank value needs to change, choose to get the original sub-classification general to the rank value, that is, the rank value of the classification plus its ID value//To facilitate the replacement of MySQL statement to 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 is simple, this will not repeat, mention a little, remember before the deletion of the class to confirm the existence of sub-class on it.

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

The above is php+mysql to achieve the infinite classification of the method, I hope that everyone's learning has helped.

http://www.bkjia.com/PHPjc/1070274.html www.bkjia.com true http://www.bkjia.com/PHPjc/1070274.html techarticle Php+mysql realize Infinite class classification, Phpmysql infinite level project thinking Analysis: A PHP project to use the classification, but not sure sub-level, so you want to make unlimited classification. 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.