PHP graphic tutorial for Infinitus classification, _ PHP Tutorial

Source: Internet
Author: User
PHP implements an Infinitus classification graphic tutorial ,. PHP graphic tutorial for Infinitus classification. Generally, the implementation of Infinitus classification uses recursive or iterative methods. let's take a look at the implementation methods in this article. 1. Database Design: PHP graphic tutorial for Infinitus classification,

Generally, the implementation of Infinitus classification uses recursive or iterative methods. let's take a look at the implementation methods in this article.

1. database design:

2. 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 ');
/**
* Obtain sub-classes from the top layer step 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 + 1 before entering the subclass -- $ deep; // depth-1} after exiting from the subclass}
Return $ lists;
}
Function displayLists ($ pid = 0, $ selectid = 1 ){
$ Result = getLists ($ pid );
$ Str ='';Foreach ($ result as $ item ){$ Selected = "";If ($ selectid = $ item ['id']) {$ Selected = 'selected ';}$ Str. =''. $ Item ['catename'].'';}Return $ str. ='';
}/**
* Obtain the parent class from the subclass level up
* @ 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 the parent class to the 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 ,:

Isn't it easy? you can use it directly without the copyright fee ^_^

In general, the implementation of Infinitus classification uses recursive or iterative methods. let's take a look at the implementation methods in this article. 1. Database Design :...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.