Infinite recursive classification query in php

Source: Internet
Author: User
The word "infinite recursive Classification Query" sounds very advanced. the literal meaning is to query all the categories, "recursion" refers to a function form used in queries-recursive functions.

The word "infinite recursive Classification Query" sounds very advanced. the literal meaning is to query all the categories, recursion refers to a function form used in a query-a recursive function. Classification is the "product" category of a website. generally, a data table is created separately by category, such as document category, product category, and image category.

Some people have doubts: Can I use SQL statements to query all classification records? However, our goal is not only to identify all categories, but more importantly, to "identify by category level", for example:

1. trousers
  1. Trousers
  2. Cowboy
2. computers
  1. Notebook
    1. asus
    2. Lenovo
  2. Desktop
3. furniture
  1. Table
    1. dining table
    1. square table
    2. Round Table
    2. desk
  2. Chair
  3. Sofa

The above example is the final result we want. However, in the actual classification data table, we cannot classify each level 1, Level 2, and Level 3 ...... The data tables are stored in the pre-configured order, so the directly queried categories are very likely to be messy. The infinite recursive classification query solves this problem. it can sort and indent the categories, just like the example above. The reference functions are as follows:

/**
* Star model training copyright: unlimited recursive Classification Query
* ===================================================== ==================================
* The CMS background management system mainly includes the following:
*
* 1. message management 2. article Management 3. image management
* 4. Category Management 5. navigation management 6. link management
* 7. Template Management 8. user management 9. system settings
*
* Future expansion-> Website product management (for example, products)
* ===================================================== ==================================
* @ File: getSort. php
* @ Author: luchanghong
* @ E-mail: luchanghong@xingmo.com
* @ Date: 2011/6/11
* @ Update:
* @ Copyright: 2002-2011 Beijing xingmo practical training school
**/

/*--------------------------------------------------------------------------/
+ @ Function: infinite recursive Classification Query
+
+ @ Parameter: $ parent_id (parent id), $ table (Category Data table name), $ n (indent)
+
+ @ Return: returns a two-dimensional array that stores classes (id and name) in order)
/--------------------------------------------------------------------------*/
Function getSort ($ parent_id, $ table, $ n =-1)
{
Static $ sort_array = array ();
$ SQL = "SELECT * FROM '{$ table} 'where' parent _ id' =' {$ parent_id }'";
$ Query = mysql_query ($ SQL );
$ N ++;
While ($ res = mysql_fetch_assoc ($ query ))
{
$ Res ['sort _ name'] = str_repeat ('', 4 * $ n). $ res ['sort _ name'];
$ Sort_array [] = $ res;
$ Sort_id = $ res ['sort _ id'];
GetSort ($ sort_id, $ table, $ n );
}
Return $ sort_array;
}

?>

 

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.