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 ============