PHP unlimited class query parent-layer functions

Source: Internet
Author: User
ClassIDclassFIDclassNameclassCount10 China 021 Zhejiang 031 Jiangsu 042 Hangzhou 054 Xihu District 0 if findFather ('4', '0') show = Hangzhou findFather ('4', '1 ') display = Zhejiang findFather ('4', '2'

ClassID classFID className classCount
1 0 China 0
2 1 Zhejiang 0
3 1 Jiangsu 0
4 2 Hangzhou 0
5 4 Xihu District 0

If

FindFather ('4', '0') display => Hangzhou

FindFather ('4', '1') display => Zhejiang

FindFather ('4', '2') display => China

FindFather ('4', '3') display => China-> Zhejiang-> Hangzhou

The code is as follows:

// =========== FindFather function START ==============
// Function: unlimitedly classify to find relevant data of the parent layer
// Parameter: $ classID, ID of the current child layer
// $ Type, 0 find yourself 1 Find Father 2 find ancestor 3 find Genealogy
// Field: primary key of the classID, which is the parent ID of the automatically generated classFID.
// ClassName category name classCount category Statistics
Function findFather ($ classID, $ type)
{
Global $ db, $ flist, $ forefather;
Define ("_ STR_CUT", "-> ");

$ Db-> query ("set names 'utf8 '");
$ SQL = 'select * from tbl_name where classID = "'. $ classID .'"';
$ Result = $ db-> query ($ SQL );
$ RecordCount = $ result-> num_rows;
If ($ recordCount! = 0)
{
// Value
$ Row = $ result-> fetch_assoc ();
$ ClassFID = $ row ['classfid'];
$ ClassID = $ row ['classid '];
$ ClassName = $ row ['classname'];

// If the ancestor is found, that is, the classFID is 0, the function state is set to 0.
If ($ classFID = '0') $ type = '0 ';
}

If ($ type = '1') // find the parent
{
$ Type = '0'; // The function State starts for the second time and is 0, that is, two cycles.
FindFather ($ classFID, $ type );
}
Else if ($ classFID! = '0' AND $ type = '2') // find the ancestor. the status type is 2 AND the ancestor classFID is not 0.
{
FindFather ($ classFID, $ type );
}
Else if ($ type = '3 ')
{
FindFather ($ classFID, $ type );
$ Flist = $ flist. _ STR_CUT. $ className; // Generate a family tree
}
Else if ($ type = '0 ')
{
$ Forefather = $ className;
}

$ Result = $ forefather. $ flist;
Return $ result;

}
// =========== FindFather function END ============

 

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.