Find a recursive algorithm solution

Source: Internet
Author: User
Find a recursive algorithm id & nbsp; name & nbsp; pid1 & nbsp; & nbsp; AAA & nbsp; & nbsp find a recursive algorithm
Id name pid
1 AAA 0
2 BBB 1
3 CCC 2
4 DDD 2
5 EEE 0
6 FFF 5
7 GGG 6

Obtain

AAA
BBB
CCC => 'CCC'
DDD => 'ddd'
EEE
FFF
GGG => 'GGG'
------ Solution --------------------
Http://bbs.csdn.net/topics/390731721
------ Solution --------------------
function tree($pid=0) {
  $res = array();
  $rs = mysql_query("select * from tbl_name where pid=$pid");
  while($row = mysql_fetch_assoc($rs)) {
    $res[$row['name']] = ($t = tree($row['id'])) ? $t : $row['name'];
  }
  return $res;
}

------ Solution --------------------
The principle is the same. the landlord can modify the code in the link on the first floor to meet the requirements.

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.