Php unlimited classification [recursion]-PHP source code

Source: Internet
Author: User
Infinitus classification is generally used for product or site product classification. Let's take a look at the algorithms and code for using recursive methods. Infinitus classification is generally used for product or site product classification. Let's take a look at the algorithms and code for using recursive methods.

Script ec (2); script

// $ Parent is the parent of the children we want to see

// $ Level is increased when we go deeper into the tree,

// Used to display a nice indented tree

Function display_children ($ parent, $ level ){

// Obtain all child nodes of a parent node $ parent

$ Result = mysql_query ("

SELECT name

FROM tree

WHERE parent = '". $ parent ."'

;"

);

// Display each subnode

While ($ row = mysql_fetch_array ($ result )){

// Display the node name in indentation

Echo str_repeat ('', $ level). $ row ['name']." n ";

// Call this function again to display the subnode of the subnode

Display_children ($ row ['name'], $ level + 1 );

}

}

?>


Use this function for the root node (Food) of the entire structure to print the entire multi-level tree structure. Because Food is the root node, its parent node is empty, so we call it like this: display_children ('', 0 ). The contents of the entire tree are displayed: Food

Fruit

Red

Cherry

Yellow

Banana

Meat

Beef

Pork

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.