Steps for reading tree data

Source: Internet
Author: User
The method for reading tree data the method for reading tree data is generally used to read or read data from arrays layer by layer recursively when the data is organized by the "adjacent list algorithm, then use recursive or non-recursive methods to process the data again. here we will introduce a method to generate a "tree" array while reading, hoping to use mysql_connect (); to read the tree data.
How to Read tree data
Generally, recursive layer-by-layer reading is required when reading data organized by using the "adjacent list algorithm ".
Or read the data to the array, and then use recursive or non-recursive methods for processing.
This section describes how to generate a "tree" array while reading it.
Mysql_connect ();

// Test data
$ SQL = <SQL
Select * from (
Select '1' as id, '0' as pid, 'food' as title
Union all select '2', '1', 'fruit'
Union all select '3', '2', 'Red'
Union all select '4', '3', 'cherry'
Union all select '5', '2', 'yellow'
Union all select '6', '5', 'bana'
Union all select '7', '1', 'meat'
Union all select '8', '7', 'Beef'
Union all select '9', '7', 'pork'
) T
Order by pid, id
SQL;

$ Rs = mysql_query ($ SQL );

$ Res = array (); // result array
$ Ind = array (); // index array
While ($ row = mysql_fetch_assoc ($ rs )){
List ($ id, $ pid) = array_values ($ row );
$ Ind [$ id] = $ row;
If (isset ($ ind [$ pid]) $ ind [$ pid] ['child '] [$ id] = & $ ind [$ id]; // construct an index
If ($ pid = 0) $ res [$ id] = & $ ind [$ id]; // switch to the stub node group
}

Echo' '&amp; Nbsp ;. &amp; nbsp; print_r ($ res, &amp; nbsp; 1); &lt;br/&gt; &lt;/pre&gt; Array &lt;br/&gt; (&lt;br/&gt; &amp; nbsp; &amp; nbsp; [1] &amp; nbsp ;=&gt;&amp; nbsp; Array &lt;br/&gt; &amp; nbsp; &amp; nbsp; (&lt;br/&gt; &amp; nbsp; &amp; nbsp; [id] &amp; nbsp ;=&gt;&amp; nbsp; 1 &lt;br/&gt; &amp; nbsp; &amp; nbsp; [pid] &amp; nbsp ;=&gt;&amp; nbsp; 0 &lt;br/&gt; &amp; nbsp; &amp; nbsp; [title] &amp; nbsp; =&gt; &amp; nbsp; Food &lt;br/&gt; &amp; nbsp; &amp; nbsp; [child] &amp; nbsp ;=&gt;&amp; nbsp; Array &lt;br/&gt; &amp; nbsp; &amp; nbsp; (&lt;br/&gt; &amp; nbsp; &amp; nbsp; &amp; nbsp; [2] &amp; nbsp ;=&gt;&amp; nbsp; Array &lt;br/&gt; &amp; nbsp; &amp; nbsp; &amp; nbsp; (&lt;br/&gt; &amp; nbsp; &amp; nbsp; &amp; nbsp; [id] &amp; nbsp ;=&gt;&amp; nbsp; 2 &lt;br/&gt; &amp; nbsp; &amp; nbsp; &amp; nbsp; [pid] &amp; nbsp ;=&gt;&amp; nbsp; 1 &lt;br/&gt; &amp; nbsp; &amp; nbsp; &amp; nbsp; &amp; nbsp; [title] &amp; nbsp ;=&gt;&amp; nbsp; Fruit &lt;br/&gt; &amp; nbsp; &amp; nbsp; &amp; nbsp; [child] &amp; nbsp ;=&gt;&amp; nbsp; array &lt;br/&gt; &amp; nbsp; &amp; nbsp; &amp; nbsp; (&lt;br/&gt; &amp; nbsp; &amp; nbsp; &amp; nbsp; &amp; nbsp; [3] &amp; nbsp ;=&gt;&amp; nbsp; Array &lt;br/&gt; &amp; nbsp; &amp; nbsp; &amp; nbsp; &amp; nbsp ;(

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.