PHP Infinite Pole classification
PHP Unlimited pole classification is often used, I have been using has been written well, so did not carefully studied, today because of the needs of the project, the need to temporarily do a PHP infinite classification of the east, then made a most simple, is now recorded as follows, there is a need for friends to see.
Data table structure
CREATE TABLE IF not EXISTS ' category ' ( ' id ' int (5) is not NULL auto_increment COMMENT ' unique self-increment id ', ' pid ' int (5) Not NUL L default ' 0 ' COMMENT ' parent id ', ' sort ' int (2) NOT null default ' 0 ' COMMENT ' sort number ', ' name ' varchar () Default NULL CO Mment ' name ', PRIMARY KEY (' id ')) engine=myisam DEFAULT Charset=utf8 comment= ' infinite pole classification table ' auto_increment=1;
Data
INSERT into ' category ' (' id ', ' pid ', ' Sort ', ' name '), VALUES (1, 0, 1, ' PHP '), (2, 0, 2, ' database '), (3, 0, 3, ' JavaScript '), (4), 1 , 1, ' Framework template '), (5, 1, 2, ' function Summary '), (6, 2, 1, ' MySQL '), (7, 4, 1, ' frame '), (8, 4, 2, ' template '), (9, 8, 1, ' Smarty '), (+, 7, 2, ' thinkphp '), (one, ten, 1, ' thinkphp techniques '), (A. 2, ' thinkphp template '), (13, 12, 3, ' template knowledge Summary '), (14, 12, 2, ' template video Tutorial '), (all, one, 1, ' model tips ');
Function Implementation Code
Function tree (& $list, $pid =0, $level =0, $html = '--') { static $tree =array (); foreach ($list as $v) { if ($v [' pid ']== $pid) { $v [' Level ']= $level; $v [' HTML ']=str_repeat ($html, $level); $tree []= $v; Tree ($list, $v [' id '], $level +1, $html); } } return $tree;}
The first parameter of the tree function above is $list the result set of a two-dimensional array of the above table. It is important to note that the SQL statement that gets the result set from the database must be prefixed with the order by sort ASC, otherwise the sort field will not function as a sort .
Articles you may be interested in
- PHP Empty (delete) the file under the specified directory, do not delete the Directory folder method
- PHP gets a method for each month in a certain period of time, returning an array of these months
- PHP gets the start timestamp and end timestamp for today, yesterday, last week, this month
- Reason analysis of invalid PHP Setcookie
- PHP get today's tomorrow yesterday time stamp of N method summary
- PHP compressed HTML page code (clear spaces, line breaks, tabs, comment markers)
- Insert and UPDATE statements for beginners in PHP construct classes
- How to write an array variable to a file in PHP
http://www.bkjia.com/PHPjc/1058849.html www.bkjia.com true http://www.bkjia.com/PHPjc/1058849.html techarticle php infinite Pole classification php infinite classification is often used, I have been using has been written well, so did not carefully studied, today because of the needs of the project, need to do a temporary ...