PHP City Unlimited Classification
ClassID Classfid className ClassCount
1 0 China 0
2 1 Zhejiang 0
3 1 Jiangsu 0
4 2 Hangzhou 0
5 4 West L. District 0
If
Findfather (' 4 ', ' 0 ') shows => Hangzhou
Findfather (' 4 ', ' 1 ') shows => Zhejiang
Findfather (' 4 ', ' 2 ') shows => China
Findfather (' 4 ', ' 3 ') shows => China-> Zhejiang-> Hangzhou
The code is as follows:
Copy Code
========== findfather function START ==========
Function: To find the relevant data of the parent layer by the infinite level classification
Parameters: $classID, number of the current child layer
$type, 0 Find yourself 1 Find your Father 2 Find your ancestors 3 Find a family tree
Fields: ClassID primary keys, generating Classfid parent numbers from
ClassName category name ClassCount classified 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)
{
Take 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 Father
{
$type = ' 0 '; The second start function state is 0, that is, the loop is 2 times
Findfather ($classFID, $type);
}
else if ($classFID!= ' 0 ' and $type = = ' 2 ')//Find ancestor, state type is 2, ancestor Classfid not 0 not found
{
Findfather ($classFID, $type);
}
else if ($type = = ' 3 ')
{
Findfather ($classFID, $type);
$flist = $flist. _str_cut. $className; Generating genealogy
}
else if ($type = = ' 0 ')
{
$forefather = $className;
}
$result = $forefather. $flist;
return $result;
}