Evaluate and modify an infinite classification recursion function

Source: Internet
Author: User
Evaluate and modify an infinite classification recursion function
// =========== FindFather function START ================/// function: infinite classification: find the relevant data of the parent layer // parameter: $ classID, number of the current child layer // $ type, 0 find yourself 1 Find Father 2 find ancestor 3 find genealogy // field: classID primary key, self-generated classFID parent Number // className category name classCount category Statistics function findFather ($ classID, $ type) {global $ flist, $ forefather; define ("_ STR_CUT ", "->"); $ SQL = 'select * from mar_menu where id = "'. $ classID. '"'; $ res1 = _ query ($ SQL); $ row = _ fetch_array ($ res1); if ($ row) {// value: $ cl AssFID = $ row ['ppid']; $ classID = $ row ['id']; $ className = $ row ['m _ title']; // if an ancestor is found, that is, if the classFID is 0, the function state is set to 0 if ($ classFID = '0') $ type = '0';} if ($ type = '1 ') // find the father {$ type = '0'; // The function status starts from 0 for the second time, that is, the findFather ($ classFID, $ type) for two cycles ); $ flist = $ flist. _ STR_CUT. $ className; // Generate Genealogy} else if ($ classFID! = '0' AND $ type = '2') // find the ancestor. the status type is 2, AND the ancestor classFID is not 0 AND cannot be found {findFather ($ classFID, $ type ); $ flist = $ flist. _ STR_CUT. $ className; // Generate Genealogy} else if ($ type = '3') {findFather ($ classFID, $ type); $ flist = $ flist. _ STR_CUT. $ className; // Generate Genealogy} else if ($ type = '0') {$ forefather = $ className;} $ result = $ forefather. $ flist; return $ result ;}



Search for all parent columns in a loop.
The function can only be changed to an infinite number of layer-3 nodes. my data may exceed layer-4 nodes.




















Reply to discussion (solution)

$ ClassFID = $ row ['ppid'];
$ ClassID = $ row ['id'];
$ ClassName = $ row ['m _ title'];

It should be
$ ClassFID = $ row ['classfid'];
$ ClassID = $ row ['classid '];
$ ClassName = $ row ['classname'];

This is wrong. sorry.

$ F = array ();
Function findFather ($ classID, $ type ){
Global $ db;
Global $ f;
For ($ t = 0; $ t <= $ type; $ t ++ ){
$ Res = $ db-> query ("select * from mar_menu where classID = $ classID ");
FindFather ($ res [0] ['classfid'], $ type-1 );
$ F [] = $ res [0] ['classname'];
Return;
}
}
FindFather (4, 1 );
Echo implode ("->", $ f );

$conn = mysql_connect("localhost", "test", "test");mysql_select_db("test", $conn);mysql_set_charset("utf8", $conn);$res = mysql_query("select * from region");$data = array();while ($row = mysql_fetch_assoc($res)){    $data[] = $row;}print_r($data);function findParents($class_id){    global $data;    static $_tree;    foreach ($data as $key => $val)    {        if ($val['classID'] == $class_id)        {            $pid = $val['classFID'];            $_tree[] = $val;            if ($pid != 0)            {                findParents($pid);            }        }    }    return $_tree;}$rs = findParents(4);krsort($rs);print_r($rs);

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.