Build a website: write a Forum using recursive functions

Source: Internet
Author: User
The implementation method of the Forum is complicated. if you analyze it, the problem will be solved. First, let's look at the implementation of the Forum. someone will post it, and then someone will post it, this relationship forms a parent-child relationship. generally, a practical Forum is written, and the Forum is formed as long as the parent-child relationship is solved. let's take a look at the method to complete the forum. First, we need to use a database to record the parent-child relationship. Generally, the implementation method of the Forum is complicated. if we analyze it, we can solve the problem, first, let's look at the implementation of the Forum. some people post the forum and then follow the post. this relationship forms a parent-child relationship. generally, we write a practical forum to solve this parent-child relationship, the forum has been formed.

Let's take a look at the method to complete the forum. First, we need to use a database to record the parent-child relationship. the general method is to do this with no duplicate id numbers, the "parentid" is used to record the id of the parent post. the id of the parent post is recorded in the sub-post storage, and the parentid of the parent post is 0, let's take a look at the database recording format:

Parent 1: id: 1 | parentid: 0
Sub1: id: 2 | parentid: 1
Child 1: id: 3 | parentid: 2
Parent 2: id: 4 | parentid: 0
Child 2: id: 5 | parentid: 4
Child 2: id: 6 | parentid: 4
......

In this way, the sub-parent relationship recorded in the database has been established, and then we will analyze how php can establish this parent-child relationship and add it to the database. when the user wants to speak, this post is a parent post, so php can set parentid to 0. when a user is following the post, php attaches the id of the parent post that the user is following to parentid, in this way, php completes the establishment of the child parent relationship.

The establishment and storage of sub-parent relationships are implemented, and the next step is to display this relationship. here we need to use recursive functions to implement it. 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 ){
// Add the ul control layer
$ 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 to use recursive functions to implement Forum methods. of course, this forum is extremely primitive. adding functional code to different parts will make the forum more powerful, if you are interested, try it yourself.

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.