Questions about PHP and MYSQL Algorithms

Source: Internet
Author: User
Known table t1 {code ...} you want to query {code ...} data is rearranged by php {code ...} get the data in the following structure {code ...} in this process, how should I write PHP code or algorithms? Please advise. Known table t1

Id txt parent -------------------------- 1 hitting a tiger in the mountains 0 2 1234 5 0 3 the tiger is not at home 1 4 La la ah 2 6 ah n en 0...

You want to query

mysql_query("SELECT id,txt,parent FROM t1 ORDER BY id DESC")

Data is rearranged by php

How should I write the code here ???

Obtain the data in the following structure:


  
  
  • Well, nun
  • September 5
    • Ah, ah
  • Fight tigers in the mountains
    • La la
    • The tiger is not at home.

In this process, how should I write PHP code or algorithms? Please advise.

Reply content:

Known table t1

Id txt parent -------------------------- 1 hitting a tiger in the mountains 0 2 1234 5 0 3 the tiger is not at home 1 4 La la ah 2 6 ah n en 0...

You want to query

mysql_query("SELECT id,txt,parent FROM t1 ORDER BY id DESC")

Data is rearranged by php

How should I write the code here ???

Obtain the data in the following structure:


  
  
  • Well, nun
  • September 5
    • Ah, ah
  • Fight tigers in the mountains
    • La la
    • The tiger is not at home.

In this process, how should I write PHP code or algorithms? Please advise.


  Array ([1] => Web Server [3] => Nginx [4] => Apache) [2] => Array ([2] => Database [5] => MySQL) [6] => Array ([6] => Browser ))

Code

/*** Infinite sublevel recursive loop * @ param [type] $ data [description] * @ param integer $ pid [description] * @ return [type] [description] */ function unlimitedForChild ($ data, $ pid = 0) {$ array = array (); foreach ($ data as $ value) {if ($ value ['parent'] = $ pid) {$ value ['child '] = unlimitedForChild ($ data, $ value ['id']); $ array [] = $ value ;}} return $ array ;} $ data = array ('id' => 1, 'txt '=> 'Go up the client', 'parent' => 0 ), array ('id' => 2, 'txt '=> 'August 5', 'parent' => 0), array ('id' => 3, 'txt '=> 'tiger not at home', 'parent' => 1), array ('id' => 4, 'txt '=> 'la la Lala ', 'parent' => 1), array ('id' => 5, 'txt '=>', 'parent' => 2 ), array ('id' => 6, 'txt '=> 'hmm nen', 'parent' => 0 ), // append Level 3 array ('id' => 7, 'txt '=> 'level 3', 'parent' => 3 ),); $ result = unlimitedForChild ($ data); echo'
';print_r($result);echo '
';
Result
Array ([0] => Array ([id] => 1 [txt] => hitting a tiger in the mountains [parent] => 0 [child] => Array ([0] => array ([id] => 3 [txt] => tiger is not at home [parent] => 1 [child] => Array ([0] => Array ([id] => 7 [txt] => Level 3 [parent] => 3 [child] => Array ()))) [1] => Array ([id] => 4 [txt] => La la [parent] => 1 [child] => Array ()))) [1] => Array ([id] => 2 [txt] => February 5, 1234 [parent] => 0 [child] => Array ([0] => Array ([id] => 5 [txt] => ah [parent] => 2 [child] => Array ()))) [2] => Array ([id] => 6 [txt] => hmm nun [parent] => 0 [child] => Array ()))


   Array        (            [name] => Web Server            [sub] => Array                (                    [3] => Array                        (                            [name] => Nginx                        )                    [4] => Array                        (                            [name] => Apache                        )                )        )    [2] => Array        (            [name] => Database            [sub] => Array                (                    [5] => Array                        (                            [name] => MySQL                        )                )        )    [6] => Array        (            [name] => Browser        ))

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.