How is the classic PHP Forum program written? As a Forum programmer, I would like to write about the experiences of Forum compilation in my spare time.
Taking the self-compiled open-source forum as an example, the development focus is on the first step. how to arrange the publishing blocks and write them out is quite satisfactory.
The forum is divided into major sections. There are various small sections below, and there may be subsections below the small sections. To reduce the difficulty, skip the section.
The design library is divided into two tables.
Two fields in the forum
Id => PRIMARY KEY
Bzone => forum name
Five fields in the forum
Id => PRIMARY KEY
Szone => small forum name
Mark => small forum description
Bid => big Forum ID, corresponding to the primary key of the big Forum
Ssort => sorting of small sections, which can affect the sorting of large sections
With the design of these two tables, can we get all the functions of forum sections? The answer is yes.
Code implementation
$sql_block = "select * from " . C('db_prefix') . "small_block order by ssort desc,bid desc,szone desc"; $query_block = mysql_query($sql_block); $data_block = array(); $data_big = array(); $time1 = date("Y-m-d"); $time1 .= " 00:00:00"; $time2 = date("Y-m-d"); $time2 .= " 23:59:59"; while ($arr_block = mysql_fetch_array($query_block)) { $data_block[] = $arr_block; $bsql = "select * from " . C('db_prefix') . "big_block where id=" . $arr_block['bid']; $barr = mysql_fetch_array(mysql_query($bsql)); $data_big[$arr_block['bid']] = $barr['bzone']; ${'zhuti' . $arr_block['id']} = mysql_num_rows(mysql_query("select * from " . C('db_prefix') . "talk where parentid=" . $arr_block['id'])); $this->assign("zhuti" . $arr_block['id'], ${'zhuti' . $arr_block['id']}); ${'tiezi1' . $arr_block['id']} = mysql_num_rows(mysql_query("select * from " . C('db_prefix') . "talk where parentid=" . $arr_block['id'])); ${'tiezi2' . $arr_block['id']} = mysql_num_rows(mysql_query("select * from " . C('db_prefix') . "reply where parentid2=" . $arr_block['id'])); ${'tiezi' . $arr_block['id']} = ${'tiezi1' . $arr_block['id']} + ${'tiezi2' . $arr_block['id']}; $this->assign("tiezi" . $arr_block['id'], ${'tiezi' . $arr_block['id']}); ${'arc' . $arr_block['id']} = $this->find(C('db_prefix') . "talk", "string", "parentid=" . $arr_block['id'] . " order by timeup desc"); $this->assign("arc" . $arr_block['id'], ${'arc' . $arr_block['id']}); ${'today1' . $arr_block['id']} = $this->select("select count(*) as count1 from " . C('db_prefix') . "talk where parentid in " . "(".$arr_block['id'].",".(int)($arr_block['id']+10000).")". " and time1 between '" . $time1 . "' and '" . $time2 . "'"); ${'today2' . $arr_block['id']} = $this->select("select count(*) as count2 from " . C('db_prefix') . "reply where parentid2 in" . "(".$arr_block['id'].",".(int)($arr_block['id']+10000).")". " and time2 between '" . $time1 . "' and '" . $time2 . "'"); ${'today' . $arr_block['id']} = ${'today1' . $arr_block['id']}[0][count1] + ${'today2' . $arr_block['id']}[0][count2]; $this->assign("today" . $arr_block['id'], ${'today' . $arr_block['id']}); }
We will remove some other redundant information, regardless of the number of posts today, the number of posts and other information, read the forum content separately.
$sql_block = "select * from " . C('db_prefix') . "small_block order by ssort desc,bid desc,szone desc";$query_block = mysql_query($sql_block);$data_block = array();$data_big = array();while ($arr_block = mysql_fetch_array($query_block)) { $data_block[] = $arr_block; $bsql = "select * from " . C('db_prefix') . "big_block where id=" . $arr_block['bid']; $barr = mysql_fetch_array(mysql_query($bsql)); $data_big[$arr_block['bid']] = $barr['bzone']; }
Query small sections and perform cyclic processing. store all the small sections in the $ data_block array.
Then, query the large Forum according to the content of the small forum and store it in an array $ data_big. The key name is "big Forum ID ".
After obtaining these variables, you must output them in the template.
$this->assign('data_big', $data_big) ->assign('data_block', $data_block);
Output in template
Array_array ['data _ big ']) {foreach ($ this-> array_array ['data _ big'] as $ k = >$ n) {?> // You can output $ k (large Forum ID) or $ n (large forum name) in a custom rich style)
Array_two ['data _ Block']) {foreach ($ this-> array_two ['data _ Block'] as $ v) {if ($ v ['bid']! = $ K) continue;?> // You can output the traversal $ v in a custom rich style. The small section name is $ v ['szone'].
Conclusion: some of the details about today's posts, the total number of posts, and the total number of posts are not described in detail, when the forum is output, it is very clever to combine the small forum ID for output. After this function is implemented, the list page and content page can be expanded based on the ID of the small section.
Reply to discussion (solution)
Good stuff
Thank you for sharing. a good tutorial.
"Small sections can affect the sorting of large sections." What does this mean?
"Small sections can affect the sorting of large sections." What does this mean?
That is
For example
Large Forum country small forum China (sorting ID = 10) United States (sorting ID = 20)
Large Forum City small Forum Beijing (sorting ID = 10) Shanghai (sorting ID = 21)
How do you think it is sorted?
1. Shanghai has the largest ID, so the largest sections are listed at the top of the list by city.
2. Shanghai is larger than Beijing, with smaller sections listed at the top of the list.
The design library is divided into two tables.
......
Why not use a table directly, and use a fid to identify whether there are subforum and subforum IDs?
Reply from xjl756213616: The design library is divided into two tables.
......
Why not use a table directly, and use a fid to identify whether there are subforum and subforum IDs?
I have never tried it ~ Table separation is easy to operate because it also involves forum management in the background.
Reference the reply from nowphp on the fifth floor: reference the reply from xjl756213616: Design Library design is divided into two tables
......
Why not use a table directly, and use a fid to identify whether there are subforum and subforum IDs?
I have never tried it ~ Table separation is easy to operate because it also involves forum management in the background.
Haha, I just gave my opinion for your reference. If your small forum has a subforum, it is estimated that you need a better table. Because it is impossible to create a new table, right.
Such as; category Table: id, fid, name, leftid (previous forum), rightid (next Forum), banzhu