PHP implementation of Infinitus classification graphic tutorial,
Generally speaking, recursive or iterative methods are used to implement Infinitus classification. Friends, please take a look at the implementation method in this article.
1. Database design:
2, code:
Copy code The code is as follows:
/** * @author koma * @todo PHP Infinitus Classification */ $cn = mysql_connect('localhost', 'root', '') or die(mysql_error()); mysql_select_db('t', $cn) or die(mysql_error()); mysql_query('set names utf8'); /** * Get subclasses from the top level down * @param number $pid * @param array $lists * @param number $deep * @return array * / function getLists($pid = 0, &$lists = array(), $deep = 1) { $sql = 'SELECT * FROM category WHERE pid='.$pid; $res = mysql_query( $sql); while ( ($row = mysql_fetch_assoc($res)) !== FALSE ) { $row['catename'] = str_repeat(' ', $deep).'|--- '.$row['catename']; $lists[] = $row; getLists($row['id'], $lists, ++$deep); //Depth before entering the subclass +1 and result = getLists($pid); $str = ''; } /** * Start from the subclass and get its parent class step by step * @param number $cid * @param array $category * @return array: */ function getCategory($cid, & $category = array()) { $sql = 'SELECT * FROM category WHERE id='.$cid.' LIMIT 1'; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); if ( $row ) { $category[] = $row; getCategory($row['pid'], $category); } krsort($category); //Reverse order to achieve the effect from parent class to subclass Return $category; } function displayCategory($cid) { $result = getCategory($cid); $str = ""; foreach ( $result as $item ) { $str .= ''. $item['catename'].'>'; } return substr($str, 0, strlen($str) - 1); } echo displayLists(0, 3); echo displayCategory(13);
3, rendering:
Isn’t it very simple? Friends can use it directly without any copyright fees ^_^
http: //www.bkjia.com/PHPjc/917041.htmlTechArticlePHP implementation of Infinitus classification graphic tutorial. Generally speaking, recursive or iterative methods are used to implement Infinitus classification. , friends, let’s take a look at the implementation of this article. 1. Database design:...
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn