Two methods for implementing tree structure using PHP program

Source: Internet
Author: User
1. the method of delivery refers to explicitly calling itself in a function. The implementation of the tree structure using the progressive method is characterized by fast data writing speed and slow display speed (especially when there are many branches/layers of the tree ). Large amount of practical and written data 1. return method
A progressive callback is an explicit call to itself in a function.
The implementation of the tree structure using the progressive method is characterized by fast data writing speed and slow display speed (especially when there are many branches/layers of the tree ). It is useful when a large amount of data is written, and the tree structure is complex.
Data structure (take mysql as an example)

Code:
Create table 'tree1 '(
'Id' tinyint (3) unsigned not null auto_increment,
'Parentid' tinyint (3) unsigned not null default '0 ',
'Topic 'varchar (50) default NULL,
Primary key ('id '),
KEY 'parentid' ('parentid ')
) TYPE = MyISAM;

Insert into 'tree1 '('id', 'parentid', 'topic') valueS
(1, 0, 'Tree 1 '),
(2, 0, 'Tree 2 '),
(3,0, 'Tree 3 '),
(4,2, 'Tree 2-1 '),
(5, 4, 'Tree 2-1-1 '),
(6, 2, 'Tree 2-2 '),
(7, 1, 'Tree 1-1 '),
(8, 1, 'Tree 1-2 '),
(9, 1, 'Tree 1-3 '),
(, 'Tree 1-2-1 '),
(, 'Tree 1-1-1 '),
(, 'Tree 1-1-1-1 ');

  
Field clarification
Id, id of the record
Parentid, the record's parent record id (0 is the root record)
Topic, the title of the record

Display program

Sequence tree:

PHP:

   /* Database connection */
Mysql_connect ();
Mysql_select_db ('tree ');

/* Tree-based return function */
Function tree ($ parentid = 0 ){
/* Perform an SQL query to obtain the title and id of the record */
$ SQL = 'SELECT topic, id from tree1 where

Parentid = $ parentid order by id asc ';
$ Rs = mysql_query ($ SQL );
/* Indent */
Echo ('

    ');
    While ($ ra = mysql_fetch_row ($ rs )){
    /* Display record title */
    Echo ('
  • '. $ Ra [0].'
  • ');
    /* Progressive call */
    Tree ($ ra [1]);
    }
    Echo ('
');
}
Tree ();
?>

Reverse sequence tree:

PHP:

   /* Database connection */
Mysql_connect ();
Mysql_select_db ('tree ');

/* Tree-based return function */
Function tree ($ parentid = 0 ){
/* Perform an SQL query to obtain the title and id of the record */
$ SQL = 'SELECT topic, id from tree1 where parentid

= $ Parentid order by id desc ';
$ Rs = mysql_query ($ SQL );
/* Indent */
Echo ('

    ');
    While ($ ra = mysql_fetch_row ($ rs )){
    /* Display record title */
    Echo ('
  • '. $ Ra [0].'
  • ');
    /* Progressive call */

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.