First set up the classification information table:
[SQL]View Plaincopy
- CREATE TABLE IF not EXISTS ' category ' (
- ' CategoryId ' smallint (5) unsigned not NULL auto_increment,
- ' ParentID ' smallint (5) unsigned not NULL DEFAULT ' 0 ',
- ' CategoryName ' varchar (not NULL) ,
- PRIMARY KEY (' categoryId ')
- ) ;
Insert several data:
[SQL]View Plaincopy
- INSERT into ' category ' (' categoryId ', ' parentid ', ' CategoryName ') VALUES
- (1, 0, ' php '),
- (2, 0, ' java '),
- (3, 0, ' C + + '),
- (4, 1, ' PHP basics '),
- (5, 1, ' PHP open source data '),
- (6, 1, ' PHP framework '),
- (7, 2, ' Java Se '),
- (8, 2, ' Java EE '),
- (9, 2, ' Java Me '),
- (3, ' C + + BASIC programming '),
- (One, 3, ' development of the C + + system '),
- (3, ' C embedded Programming '),
- (3, ' C + + application development '),
- ( +, ' C + + desktop application development '),
- ( D, ' C + + game development ');
Here is the PHP code:
[PHP]View Plaincopy
- <?php
- /*
[PHP]View Plaincopy
- PHP Infinite Pole classification
[PHP]View Plaincopy
- */
- Get the direct subcategory of a category
- function Getsons ($categorys,$catId =0) {
- $sons =Array ();
- foreach ($categorys as $item) {
- if ($item [' ParentID ']==$catId)
- $sons []=$item;
- }
- return $sons;
- }
- Get all subcategories of a category
- function Getsubs ($categorys,$catId =0,$level =1) {
- $subs =Array ();
- foreach ($categorys as $item) {
- if ($item [' ParentID ']==$catId) {
- $item [' Level ']=$level;
- $subs []=$item;
- $subs =array_merge ($subs, Getsubs ($categorys,$item [' categoryId '],$level + 1));
- }
- }
- return $subs;
- }
- Get all the parent categories for a category
- Method One, recursive
- function getparents ($categorys,$catId) {
- $tree =Array ();
- foreach ($categorys as $item) {
- if ($item [' categoryId ']==$catId) {
- if ($item [' ParentID ']>0)
- $tree =array_merge ($tree, getparents ($categorys,$item [' ParentID ']);
- $tree []=$item;
- Break ;
- }
- }
- return $tree;
- }
- Method Two, iteration
- function getParents2 ($categorys,$catId) {
- $tree =Array ();
- While ($catId! = 0) {
- foreach ($categorys as $item) {
- if ($item [' categoryId ']==$catId) {
- $tree []=$item;
- $catId =$item [' ParentID '];
- Break ;
- }
- }
- }
- return $tree;
- }
- Test section
- $pdo =New PDO (' mysql:host=localhost;dbname=test ',' root ', '8888 ');
- $stmt =$pdo->query ("SELECT * from category ORDER by CategoryId");
- $categorys =$stmt->fetchall (PDO::FETCH_ASSOC);
- $result =getsons ($categorys, 1);
- foreach ($result as $item)
- echo $item [' CategoryName '].' <br> ';
- echo '
- $result =getsubs ($categorys, 0);
- foreach ($result as $item)
- echo str_repeat (',$item [' Level ']). $item [' CategoryName '].' <br> ';
- echo '
- $result =getparents ($categorys, 7);
- foreach ($result as $item)
- echo $item [' CategoryName '].' >> ';
- echo '
- $result =getparents2 ($categorys, 15);
- foreach ($result as $item)
- echo $item [' CategoryName '].' >> ';
- ?>
Here is the result of the run:
Article Source: http://blog.csdn.net/kankan231/article/details/8462349