PHP unlimited classification instance project_php tutorial

Source: Internet
Author: User
PHP unlimited classification instance program. The principle of unlimited classification: Just like creating a folder under windows, you can create a folder under the new folder, so that the infinite loop continues, the same is true for unlimited classification, the principle that a parent class can be infinitely classified is as follows: create a folder in windows and create a folder in the new folder, so that the infinite loop is continued, the parent class can be divided into child classes, and child classes can be divided into child classes, so that the infinite loop continues.

Example 1

The code is as follows:

$ YArr = array (
1 => array ('id' => '1', 'parentid' => 0, 'name' => 'Level 1 topic 1 '),
2 => array ('id' => '2', 'parentid' => 0, 'name' => 'Level 1 column 2 '),
3 => array ('id' => '3', 'parentid' => 1, 'name' => 'second-level topic 1 '),
4 => array ('id' => '4', 'parentid' => 1, 'name' => 'second-level column 2 '),
5 => array ('id' => '5', 'parentid' => 2, 'name' => 'second-level column 3 '),
6 => array ('id' => '6', 'parentid' => 3, 'name' => 'Level 3 topic 1 '),
7 => array ('id' => '7', 'parentid' => 3, 'name' => 'third-level column 2 '),
8 => array ('id' => '8', 'parentid' => 2, 'name' => 'second-level column 3 '),
);

/**
* Obtain the Subid of the current id.
* @ Param array $ original data array
* @ Param int $ id current id
* @ Param int $ current layer
*/
Function genCate ($ data, $ pid = 0, $ level = 0)
{
If ($ level = 10) break;
$ L = str_repeat ("", $ level );
$ L = $ l. 'hangzhou ';
Static $ arrcat = array ();
$ Arrcat = empty ($ level )? Array (): $ arrcat;
Foreach ($ data as $ k => $ row)
{
/**
* If the parent ID is the input id
*/
If ($ row ['parentid'] = $ pid)
{
// If the id of the current traversal is not empty
$ Row ['name'] = $ l. $ row ['name'];
$ Row ['level'] = $ level;
$ Arrcat [] = $ row;
// Var_array ($ arr );
GenCate ($ data, $ psiff, $ row ['id'], $ level + 1); // recursive call
}
}
Return $ arrcat;
}

$ Carr = genCate ($ yArr );
Echo"";Foreach ($ carr as $ row){Echo"";Echo $ row ['name'];Echo"";}Echo"";

Note: Because it is an infinite call, I added a judgment to let him jump out when the level $ level = 10. No normal website has more than 10 layers

Directory structure.

After the static variable is executed, determine the current level. if the level is 0, it indicates that this is the top level menu and the data of $ arrcate needs to be cleared and declared again.


Example 2

The code is as follows:

// Create a table "class"
Create table 'class '(
'Id' int (11) not null auto_increment COMMENT 'Category ID ',
'F _ id' int (11) not null comment 'parent ID ',
'Name' varchar (25) collate gbk_bin not null comment 'Category name ',
Primary key ('id ')
) ENGINE = InnoDB default charset = gbk COLLATE = gbk_bin AUTO_INCREMENT = 1;

Code

The code is as follows:

<? Php

Header ("Content-type: text/html; charset = utf-8 ");

$ Db = new mysqli ("localhost", "root", "", "news_php100 ");
// Instantiate a database connection. Before using this function, make sure that the mysqli class library has been loaded,
Or use mysql_connect to connect.

If (mysqli_connect_errno ()){

Echo "link failed:". mysqli_connect_error ();

Exit ();}

$ Db-> query ("set names utf8 ");

$ Result = $ db-> query ("select name from class where f_id = 0 ");
// Search for the f_id = 0 category, that is, find each category.

While ($ row = $ result-> fetch_assoc ()){

Echo $ row ['name']. "<br>"; // loop every category.

}

// We can loop the subcategories of news.

$ Result = $ db-> query ("select * from class where f_id = 1 ");
// Search for the classification of f_id = 1, that is, find the subclass of 'news.

While ($ row = $ result-> fetch_assoc ()){

Echo $ row ['name']."

"; // In this way, the subcategory of 'news' is recycled. Note: it is only a subclass, not a child class.

}

// Write it here and we will find a problem. If this category is of the 10th level, do we need to write
Are each of the 10 loops repeating? If it is more multi-level classification, it is obviously unrealistic to write.

// What can be done? We can write a recursive function and pass f_id as a parameter,

Continuously loops the value of each f_id, that is, loops the subclass of each f_id value.

// First, we store the values of each classification in a two-dimensional array, which is useful in the following recursive functions.

The code is as follows:

$ Result = $ db-> query ("select * from class ");

While ($ row = $ result-> fetch_assoc ()){

$ Arr [] = array ($ row [id], $ row [f_id], $ row [name]); // save one
Category id, f_id, and name.

}

Function fenlei ($ f_id = 0) {// $ f_id is initialized to 0, that is, the cycle starts from the maximum classification.

Global $ arr; // declare $ arr as a global variable for reference in the function.

For ($ I = 0; $ I <count ($ arr); $ I ++) {// Loops each category.

If ($ arr [$ I] [1] = $ f_id) {// $ arr [$ I] [1] indicates the f_id value of the $ I + 1 category.
Start with $ f_id = 0, that is, output the f_id = 0 category.

Echo $ arr [$ I] [2]. "<br>"; // $ arr [$ I] [1] indicates the name of the $ I + 1 category.

Fenlei ($ arr [$ I] [0]); // $ arr [$ I] [1] indicates the id value of the $ I + 1 category. Recursion
That is, you can use your id as the f_id parameter to recycle your subclass.

}

}

}

Fenlei (); // use this function.

?>

Example 3

Php unlimited classification, support output tree chart

The code is as follows:

/**
* A common tree class can generate any tree structure
*/
Class tree
{
/**
* The two-dimensional array required to generate a tree structure
* @ Var array
*/
Var $ arr = array ();

/**
* The modifier required to generate a tree structure, which can be replaced by an image.
* @ Var array
*/
Var $ icon = array ('│', 'hangzhou', 'hangzhou ');

/**
* @ Access private
*/
Var $ ret = '';

/**
* Constructor, initialization class
* @ Param array 2-dimensional array, for example:
* Array (
* 1 => array ('id' => '1', 'parentid' => 0, 'name' => 'Level 1 topic 1 '),
* 2 => array ('id' => '2', 'parentid' => 0, 'name' => 'Level 1 column 2 '),
* 3 => array ('id' => '3', 'parentid' => 1, 'name' => 'second-level column 1 '),
* 4 => array ('id' => '4', 'parentid' => 1, 'name' => 'second-level column 2 '),
* 5 => array ('id' => '5', 'parentid' => 2, 'name' => 'second-level column 3 '),
* 6 => array ('id' => '6', 'parentid' => 3, 'name' => 'Level 3 topic 1 '),
* 7 => array ('id' => '7', 'parentid' => 3, 'name' => 'third-level column 2 ')
*)
*/
Function tree ($ arr = array ())
{
$ This-> arr = $ arr;
$ This-> ret = '';
Return is_array ($ arr );
}

/**
* Obtain the parent array.
* @ Param int
* @ Return array
*/
Function get_parent ($ myid)
{
$ Newarr = array ();
If (! Isset ($ this-> arr [$ myid]) return false;
$ Pid = $ this-> arr [$ myid] ['parentid'];
$ Pid = $ this-> arr [$ pid] ['parentid'];
If (is_array ($ this-> arr ))
{
Foreach ($ this-> arr as $ id => $)
{
If ($ a ['parentid'] = $ pid) $ newarr [$ id] = $;
}
}
Return $ newarr;
}

/**
* Obtain the child-level array.
* @ Param int
* @ Return array
*/
Function get_child ($ myid)
{
$ A = $ newarr = array ();
If (is_array ($ this-> arr ))
{
Foreach ($ this-> arr as $ id => $)
{
If ($ a ['parentid'] ==$ myid) $ newarr [$ id] = $;
}
}
Return $ newarr? $ Newarr: false;
}

/**
* Obtain the array of the current position.
* @ Param int
* @ Return array
*/
Function get_pos ($ myid, & $ newarr)
{
$ A = array ();
If (! Isset ($ this-> arr [$ myid]) return false;
$ Newarr [] = $ this-> arr [$ myid];
$ Pid = $ this-> arr [$ myid] ['parentid'];
If (isset ($ this-> arr [$ pid])
{
$ This-> get_pos ($ pid, $ newarr );
}
If (is_array ($ newarr ))
{
Krsort ($ newarr );
Foreach ($ newarr as $ v)
{
$ A [$ v ['id'] = $ v;
}
}
Return $;
}


/**
*-------------------------------------
* Tree structure
*-------------------------------------
* @ Author Midnight (Yang Yunzhou), yangyunzhou@foxmail.com
* @ Param $ myid indicates that all sub-levels under this ID are obtained.
* @ Param $ str generate the basic code of the tree structure, for example :"

$ Select> $ spacer $ name"
* @ Param $ ID the selected ID. for example, you need to use this ID when creating a tree drop-down box.
* @ Param $ adds
* @ Param $ str_group
* @ Return unknown_type
*/
Function get_tree ($ myid, $ str, $ sid = 0, $ adds = '', $ str_group = '')
{
$ Number = 1;
$ Child = $ this-> get_child ($ myid );
If (is_array ($ child ))
{
$ Total = count ($ child );
Foreach ($ child as $ id => $)
{
$ J = $ k = '';
If ($ number = $ total)
{
$ J. = $ this-> icon [2];
}
Else
{
$ J. = $ this-> icon [1];
$ K = $ adds? $ This-> icon [0]: '';
}
$ Spacer = $ adds? $ Adds. $ j :'';
$ Selected = $ id = $ sid? 'Selected ':'';
@ Extract ($ );
$ Parentid = 0 & $ str_group? Eval ("$ nstr =" $ str_group ";"): eval

("$ Nstr =" $ str ";");
$ This-> ret. = $ nstr;
$ This-> get_tree ($ id, $ str, $ sid, $ adds. $ k. '', $ str_group );
$ Number ++;
}
}
Return $ this-> ret;
}
/**
* Similar to the previous method, but multiple options are allowed.
*/
Function get_tree_multi ($ myid, $ str, $ sid = 0, $ adds = '')
{
$ Number = 1;
$ Child = $ this-> get_child ($ myid );
If (is_array ($ child ))
{
$ Total = count ($ child );
Foreach ($ child as $ id => $)
{
$ J = $ k = '';
If ($ number = $ total)
{
$ J. = $ this-> icon [2];
}
Else
{
$ J. = $ this-> icon [1];
$ K = $ adds? $ This-> icon [0]: '';
}
$ Spacer = $ adds? $ Adds. $ j :'';

$ Selected = $ this-> have ($ sid, $ id )? 'Selected ':'';
// Echo $ sid. '=>'. $ id. ':'. $ selected .'.
';
@ Extract ($ );
Eval ("$ nstr =" $ str ";");
$ This-> ret. = $ nstr;
$ This-> get_tree_multi ($ id, $ str, $ sid, $ adds. $ k .'');
$ Number ++;
}
}
Return $ this-> ret;
}

Function have ($ list, $ item ){
Return (strpos (','. $ list. ','. $ item .','));
}
}
?>

Note: No platform restrictions. you only need to inform the id, parentid, and name.
There are no platform restrictions for the above three types of infinite classification codes, but they can only be used in php. we just need to figure out the relationship between id, parentid, and name.

System.


...

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.