Build Web site: Use recursive functions to write a forum _php tutorial

Source: Internet
Author: User
The implementation of the Forum is more complex, as long as it analysis, the problem is solved, first look at the implementation of the Forum, someone posted, and then someone keep abreast, the relationship formed a parent-child relationship, generally write a practical forum, as long as the settlement of this sub-parent relationship, the forum has been formed.
To see the way to complete the forum, the first to use a database to record the relationship between the father, the general method is to use a duplicate ID number to complete, record the parent's ID number field is generally used "parentid" to record, the child paste storage records the ID number of the parent paste, and the parent paste ParentID rate is 0, see the number According to the record form of the library:
Parent 1:id:1 | parentid:0
Sub-1:id:2 | Parentid:1
Double 1:id:3 | Parentid:2
Parent 2:id:4 | parentid:0
Sub-2:id:5 | Parentid:4
Sub-2:id:6 | Parentid:4
......
So the database records of the child parent relationship has been established, and then to analyze how PHP to achieve the establishment of the parent-child relationship and add to the database, the first user wants to speak, his post as a parent paste, so PHP can set ParentID to 0, when there is user keep abreast, PHP will be the user with the ID of the parent paste Value to ParentID, so that PHP is complete, and the child-parent relationship is established.
To achieve the establishment and storage of child-parent relationships, the next step is to show this relationship, here you need to use a recursive function to implement, see the following code:
.....
$result =mysql_query ("SELECT * from table where parentid=0");
$num =mysql_numrows ($result);
if (!empty ($num)) {
for ($i =0; $i < $num; $i + +) {
$parentid =mysql_result ($result, $i, "id");
function Showchild ($parentid) {
Added UL control layer into
$result =mysql_query ("SELECT * from table where parentid= $parentid");
$numb =mysql_numrows ($result);
if (!empty ($numb)) {
for ($i =0; $i < $numb; $i + +) {
....
$parentid =mysql_result ($result, $i, "id");
....
Showchild ($parentid);
}
....
....
}
....
....
}
Showchild ($parentid);
....
....
}
....
....
}
The above code is the use of recursive functions to implement the Forum's approach, of course, this forum is extremely primitive, in different parts with functional code, it will be able to make this forum gradually strong, interested readers may wish to try their own hands.


http://www.bkjia.com/PHPjc/445183.html www.bkjia.com true http://www.bkjia.com/PHPjc/445183.html techarticle the implementation of the Forum is more complex, as long as it analysis, the problem is solved, first look at the implementation of the Forum, someone posts, and then someone keep abreast, the relationship formed a parent-child Association ...

  • 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.