How to implement infinite classification in php

藏色散人
Release: 2023-03-09 06:36:01
Original
2599 people have browsed it

php method to implement Infinitus classification: first create a PHP sample file; then write logic code; then obtain subclasses from the top level downwards; finally, start from the subclasses and obtain their parent classes step by step upwards. Infinite classification can be achieved.

How to implement infinite classification in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

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:

The code is as follows:

/** 
 * @author koma 
 * @todo   PHP无限极分类 
 */ $cn = mysql_connect('localhost', 'root', '') or die(mysql_error()); 
mysql_select_db('t', $cn) or die(mysql_error()); 
mysql_query('set names utf8'); 
 /** 
 * 从顶层逐级向下获取子类 
 * @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); //进入子类之前深度+1         --$deep; 
        //从子类退出之后深度-1    
         } 
    return $lists; 
} 
 function displayLists($pid = 0, $selectid = 1) { 
    $result = getLists($pid); 
    $str = &#39;<select>&#39;; 
    foreach ( $result as $item ) { 
        $selected = ""; 
        if ( $selectid == $item[&#39;id&#39;] ) { 
            $selected = &#39;selected&#39;; 
        } 
        $str .= &#39;<option &#39;.$selected.&#39;>&#39;.$item[&#39;catename&#39;].&#39;</option>&#39;; 
    } 
    return $str .= &#39;</select>&#39;; 
} /** 
 * 从子类开始逐级向上获取其父类 
 * @param number $cid 
 * @param array $category 
 * @return array: 
 */ function getCategory($cid, &$category = array()) { 
    $sql = &#39;SELECT * FROM category WHERE id=&#39;.$cid.&#39; LIMIT 1&#39;; 
    $result = mysql_query($sql); 
    $row = mysql_fetch_assoc($result); 
    if ( $row ) { 
        $category[] = $row; 
        getCategory($row[&#39;pid&#39;], $category); 
    } 
    krsort($category); //逆序,达到从父类到子类的效果     return $category; 
} 
 function displayCategory($cid) { 
    $result = getCategory($cid); 
    $str = ""; 
    foreach ( $result as $item ) { 
        $str .= &#39;<a href="&#39;.$item[&#39;id&#39;].&#39;">&#39;.$item[&#39;catename&#39;].&#39;</a>>&#39;; 
    } 
    return substr($str, 0, strlen($str) - 1); 
} 
 echo displayLists(0, 3); 
 echo displayCategory(13);
Copy after login

3, rendering:

Isn’t it very simple? Friends can use it directly without any copyright fees.

[Recommended learning:

PHP video tutorial]

The above is the detailed content of How to implement infinite classification in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template