Infinite classification is a problem that will be encountered in all program development, let me introduce the php+mysql implementation of an infinite class of classification procedures, the need for friends can refer to.
Here's a look at my database structure: The database name is: fa_category
| Field |
Type |
Comment |
| Cid |
Int (11) |
Category ID |
| Catename |
varchar (255) |
Category name |
| Catetype |
Int (1) |
Category type, 1 for single page, 2 for general category |
| Catdir |
varchar (255) |
English catalogue |
| Display |
Int (1) |
1 for display, 2 for not displayed |
| Keywords |
varchar (255) |
Column key Words |
| Description |
Text |
Column description |
| CTime |
Int (11) |
Creation time |
| ParentID |
Int (11) |
Parent Node ID, highest node parent node is 0 |
We use a parentid field to record the ID of the parent node, or root if ParentID is 0. Don't say much, let's see how the code is written. The function we want to implement is shown in the film:
How to show it this way? This question I think for a long time, first look at this SQL statement,
| The code is as follows |
Copy Code |
SELECT c.cat_id, C.cat_name, C.measure_unit, c.parent_id, C.is_show, C.show_in_nav, C.grade, C.sort_order, COUNT (s.cat_ ID) as Has_children From Ecs_category as C Left JOIN Ecs_category as s on s.parent_id = c.cat_id GROUP by c.cat_id ORDER by c.parent_id, C.sort_order ASC |
Use left JOIN to connect to a table and return a field has_children, which is how many child nodes are recorded.
Look at the code.
| The code is as follows |
Copy Code |
Public Function GetCategory ($catid =0, $re _type = True, $selected =0) { $db = new Public_database (); $sql = ' Select C.cid,c.catename,c.catetype,c.ctime,c.parentid,count (s.cid) as Has_children from '. __mysql_pre. ' Category as C left join '. __mysql_pre. ' Category as s on S.parentid=c.cid Group by C.cid ORDER by c.parentid ASC '; $res = $db->selecttable ($sql); $cateInfo = Self::getchildtree ($catid, $res); if ($re _type==true) { $select = "; foreach ($cateInfo as $val) { $select. = ' $select. = ($selected = = $val [' CID '])? "Selected= ' ture '": '; $select. = ' > '; if ($val [' Level ']>0) { $select. = Str_repeat (", $val [' Level '] * 4); } $select. = Htmlspecialchars (Addslashes ($val [' catename ']), ent_quotes). ''; } return $select; } Else { foreach ($cateInfo as $key = $val) { if ($val [' Level ']>0) { $cateInfo [$key] [' catename '] = "|". Str_repeat (", $val [' Level '] * 8)." └─ ". $val [' Catename ']; } } return $cateInfo; } } /** * Recursively get all child node trees by parent ID * @param int $catid higher class * @param array $arr contains all the categories * @return Array */ Public Function Getchildtree ($catid, $arr) { $level = $last _cat_id = 0; while (!empty ($arr)) { foreach ($arr as $key = $value) { $cid = $value [' CID ']; if ($level = = 0 && $last _cat_id = = 0) { if ($value [' ParentID '] > 0) { Break } $options [$cid] = $value; $options [$cid] [' level '] = $level; $options [$cid] [' id '] = $cid; $options [$cid] [' name '] = $value [' Catename ']; Unset ($arr [$key]); if ($value [' has_children '] = = 0) { Continue } $last _cat_id = $cid; $cat _id_array = Array ($cid); $level _array[$last _cat_id] = + + $level; Continue } if ($value [' parentid '] = = $last _cat_id) { $options [$cid] = $value; $options [$cid] [' level '] = $level; $options [$cid] [' id '] = $cid; $options [$cid] [' name '] = $value [' Catename ']; Unset ($arr [$key]); if ($value [' Has_children '] > 0) { if (end ($cat _id_array)! = $last _cat_id) { $cat _id_array[] = $last _cat_id; } $last _cat_id = $cid; $cat _id_array[] = $cid; $level _array[$last _cat_id] = + + $level; } } ElseIf ($value [' ParentID '] > $last _cat_id) { Break } } $count = count ($cat _id_array); if ($count > 1) { $last _cat_id = Array_pop ($cat _id_array); } ElseIf ($count = = 1) { if ($last _cat_id! = End ($cat _id_array)) { $last _cat_id = end ($cat _id_array); } Else { $level = 0; $last _cat_id = 0; $cat _id_array = Array (); Continue } } if ($last _cat_id && isset ($level _array[$last _cat_id])) { $level = $level _array[$last _cat_id]; } Else { $level = 0; } } return $options;
|
You can use a loop of smarty to show it the same effect as the picture above! What's wrong with you can give me a message.
http://www.bkjia.com/PHPjc/633090.html www.bkjia.com true http://www.bkjia.com/PHPjc/633090.html techarticle Infinite Classification is a problem that will be encountered in all program development, let me introduce the php+mysql implementation of an infinite class of classification procedures, the need for friends can refer to. Below to everyone ...