Php+mysql realization of infinite level classification _php skill

Source: Internet
Author: User

Project Thinking Analysis: a PHP project to use the classification, but not certain levels, so want to make an infinite class classification.
initially, as before, the database was built with 4 values, as follows:
ID: Self    |   PID: Parent class id   |  XID: Sorting id& nbsp;  |  ClassName: Category name
It was later thought that this was inconvenient when reading data and modified, and was especially inconvenient when the product was read, and changed to the following scenario:
A new field is added to the MySQL table, now database is as follows:
table name w_faqclass:   ID: self-added     |   PID: Parent class id   |  XID: Sort id   |  ClassName: Category name   |  rank:  ; Level
Definition:
First level category , PID is 0, rank is "/"
Level two category, PID is id,rank for first level classification. Id/of the first level category "
level three category, PID two classification Id,rank to"/Id/two level classification of Id/"
, and so on ...
1. Basic function     

/* Facilitate recursive return of an array of infinite-level categories that have been sorted do not want to use recursion can also use like to get after the sort, I am lazy, do not write the way to get, in fact, 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 $RETURNARR: Return array/function ReadClass ($datatab
  Le, $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 for a value in a table that returns only one value $datatable: Data table name $wherevalue: Criteria value $selectColumns: Query columnName $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. Adding categories (Directly to the Select option)

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

3. Modify the classification

<?php/* Note that because it is modified, this page is loaded with all the values of the current category read out, corresponding to: $pid, $rank * * $classArr = readclass (' w_faqclass ', ' 0 ', ' ASC ', Array ()
  );
  $canum = count ($CLASSARR);
  echo "<select name= ' pid ' >";
 
  echo "<option value= ' 0 ' > main classification </option>";  For ($i =0 $i < $canum; $i + +) {//Because it is modified, so the current classification can not choose their own or the following categories, add a rank value advantage ah, haha, before the single PID value of 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 "<option value= '". $CLASSARR [$i] [' ID ']. "' ". $selected." > ". $t. $CLASSARR [$i] [' classname ']."
  </option> "; echo "</select>"?>//Save operations//To be changed all subcategories of the classification must be changed, and the rank value of the original subcategory is selected, which means that the rank value of the category plus its ID value//is beneficial to M Replace if ($pid the Ysql replace statement != 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 on the simple, this is not to repeat, mention a little, remember to delete before the class to confirm the existence of subclasses on it.

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

The above is php+mysql to realize the infinite Pole classification method, hope to be helpful to everybody's study.

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.