浅析经典的PHP论坛程序是如何写成的?
作为一个论坛程序爱好者,趁空闲时间写一下编写论坛的心得。
以自己编写的开源论坛为例,开发重点是在第一步,如何排列出版块,写出这个,接下去就比较顺手了。
版块分为大版块,下面有各个小版块,小版块下面可能还有子版块。为了减少难度,子版块部分忽略不谈。
设计库设计分为2张表
大版块2个字段
id => 主键
bzone =>大版块名称
小版块5个字段
id => 主键
szone => 小版块名称
mark => 小版块说明文字
bid => 大版块ID,对应大版块的主键
ssort => 小版块排序,可以以小版块影响大版块的排序
以这2张表的设计就能够全部搞定论坛版块的功能部分?答案是肯定的
代码实现
$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']});
}
$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'];
}
$this->assign('data_big', $data_big)
->assign('data_block', $data_block);
<?php
if(!empty($this->array_array['data_big'])){
foreach($this->array_array['data_big'] as $k => $n){
?>
//此处可以以自定义丰富的样式输出$k(大版块ID)或$n(大版块名称)
<?php
if(!empty($this->array_two['data_block'])){
foreach($this->array_two['data_block'] as $v){
if($v['bid']!=$k) continue;
?>
//此处可以以自定义丰富的样式输出遍历的$v,小版块名称为$v['szone']
<?php
}
}
?>