PHP Unlimited Hierarchical Data Implementation program code _php tutorial

Source: Internet
Author: User
Tags php and mysql
Let us introduce to you some of the uses of the no-limit classification in PHP, including the Direct Unlimited classification of database and array operations, the need for friends to refer to.

Let's look at an unlimited classification of PHP and MySQL databases


To establish a database:
Id,fid,fname (both ID and FID must be numeric types and the default value of FID must be set to 0;

The code is as follows Copy Code

$stime =microtime ();

$db = @mysql_connect ("localhost", "root", "micronsky.net") or Die ("Database connection missing");
mysql_select_db ("temp", $db);
Defining a first-level classification
function MAINFL ()
{
Global $db;
$result =mysql_query ("Select Id,fid,fname from WXJFL where fid=0 order by id DESC", $db);
if ($myrow =mysql_fetch_array ($result)) {
do {
echo $myrow ["fname"];
echo "
";
Echo SUBFL ($myrow ["id"], ""); The child classification function is called here
}
while ($myrow =mysql_fetch_array ($result));
}

}
Define a child class
function SUBFL ($fid, $strdis)
{
Global $db;
$result 1=mysql_query ("Select Id,fid,fname from WXJFL where fid= $fid order by id DESC", $db);
if ($myrow 1=mysql_fetch_array ($result 1)) {
do {
Echo $strdis;
echo $strdis. $myrow 1["FName"];
echo "
";
SUBFL ($myrow 1["id"], "". $strdis); It is important to note that you do not need to echo the same function as above ... Just call the sub-classification function and pay the value directly! Also here is the recursive part
}
while ($myrow 1=mysql_fetch_array ($result 1));
}
}


Echo MAINFL ();

$ltime =microtime ();
echo "
";
Echo Number_format ($ltime-$stime, 4); Statistical execution time, here is much faster than ASP, but this is not much to do with the wording, the main reason is that PHP itself joined the accelerator!

?>


Now we're just going to use the array for paging

The code is as follows Copy Code

/**
* Create parent Node tree array
* Parameters
* $ar arrays, adjacency list-organized data
* $id the subscript or association key name in the array as the primary key
* $pid the subscript or association key name in the array as the parent key
* Returns a multidimensional array
**/
function Find_parent ($ar, $id = ' id ', $pid = ' pid ') {
foreach ($ar as $v) $t [$v [$id]] = $v;
foreach ($t as $k = = $item) {
if ($item [$pid]) {
if (! isset ($t [$item [$pid]][' parent '] [$item [$pid]])
$t [$item [$id]][' parent '] [$item [$pid]] =& $t [$item [$pid]];

$t [$k] [' reference '] = true;
}
}
return $t;
}


/**
* Create a sub-node tree array
* Parameters
* $ar arrays, adjacency list-organized data
* $id the subscript or association key name in the array as the primary key
* $pid the subscript or association key name in the array as the parent key
* Returns a multidimensional array
**/
function Find_child ($ar, $id = ' id ', $pid = ' pid ') {
foreach ($ar as $v) $t [$v [$id]] = $v;
foreach ($t as $k = = $item) {
if ($item [$pid]) {
$t [$item [$pid]][' child '] [$item [$id]] =& $t [$k];

$t [$k] [' reference '] = true;
}
}
return $t;
}

Example:

The code is as follows Copy Code
$data = Array (
Array (' ID ' =>1, ' PARENT ' =>0, ' NAME ' = ' grandfather '),
Array (' ID ' =>2, ' parent ' =>1, ' NAME ' = ' father '),
Array (' ID ' =>3, ' PARENT ' =>1, ' NAME ' = ' uncles '),
Array (' ID ' =>4, ' PARENT ' =>2, ' NAME ' = ' own '),
Array (' ID ' =>5, ' PARENT ' =>4, ' NAME ' = ' son ')
);
$p = Find_parent ($data, ' ID ', ' parent ');
$c = Find_child ($data, ' ID ', ' PARENT ');

The above two methods are to split all nodes into an array by ID, and then find their parent or children, by referencing the elements that will be split into the parent, children,

However, the referenced elements still exist in the evenly divided array, so it is best to tag those referenced elements in the actual application to avoid starting the traversal with them as root, resulting in duplication.

The code is as follows Copy Code


foreach ($p as $key = = $item) {
if ($item [' reference ']) continue;
Print_r ($item);
}

foreach ($c as $key = = $item) {
if ($item [' reference ']) continue;
Print_r ($item);
}

Recursively, when the elements of the PHP array are deleted, the array cursors are zeroed, so that some elements that have found "home" in the process have to be left in the array, unable to reduce the search scope of the successor element:

code as follows copy code


$mylist = Array (' parent_id ' =>0, ' id ' =>1),
Array (' parent_id ' =>0, ' id ' =>2),
Array (' parent_id ' =>0, ' id ' =>3),
Array (' parent_id ' =>2, ' id ' =>4),
Array (' parent_id ' =>2, ' id ' =>5),
Array (' parent_id ' =>3, ' id ' =>6),
Array (' parent_id ' =>3, ' id ' =>7),
Array (' parent_id ' =>4, ' id ' =>8),
Array (' parent_id ' =>5, ' id ' =>9),
Array (' parent_id ' =>5, ' id ' =>10)
);

function _findchildren ($list, $p _id) {//Data hierarchy,
$r = Array ();
foreach ($list as $id = = $item) {
if ($item [' parent_id '] = = $p _id) {
$length = count ($r);
$r [$length] = $item;
if ($t = $this->_findchildren ($list, $item [' ID '])) {
$r [$length] [' children '] = $t;
}
}
}
return $r;
}

Print_r (_findchildren ($mylist, 0));

http://www.bkjia.com/PHPjc/631571.html www.bkjia.com true http://www.bkjia.com/PHPjc/631571.html techarticle let us introduce to you some of the uses of the no-limit classification in PHP, including the Direct Unlimited classification of database and array operations, the need for friends to refer to. First look at a ...

  • 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.