Heim >Backend-Entwicklung >PHP-Tutorial >浅析经典的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);
<?phpif(!empty($this->array_array['data_big'])){ foreach($this->array_array['data_big'] as $k => $n){ ?> //此处可以以自定义丰富的样式输出$k(大版块ID)或$n(大版块名称) <?phpif(!empty($this->array_two['data_block'])){ foreach($this->array_two['data_block'] as $v){ if($v['bid']!=$k) continue;?> //此处可以以自定义丰富的样式输出遍历的$v,小版块名称为$v['szone']<?php } }?>
好东西
谢谢分享,很好的教程
"可以以小版块影响大版块的排序"这个是什么意思?
"可以以小版块影响大版块的排序"这个是什么意思?
意思就是
比如
大版块国家 小版块 中国(排序ID=10) 美国(排序ID=20)
大版块城市 小版块 北京(排序ID=10) 上海(排序ID=21)
你们觉得怎么排序?
1、上海ID最大,因此大版块排序以城市排列在最上面
2、上海比北京大,小版块之间,以上海排在最上面
设计库设计分为2张表
……
为什么不直接用一张表,用一个fid标示是否有子版块及子版块id
引用 楼主 xjl756425616 的回复:设计库设计分为2张表
……
为什么不直接用一张表,用一个fid标示是否有子版块及子版块id
我没尝试过~因为还有涉及到后台的版块管理,表分开会容易点操作
引用 5 楼 nowphp 的回复:引用 楼主 xjl756425616 的回复:设计库设计分为2张表
……
为什么不直接用一张表,用一个fid标示是否有子版块及子版块id
我没尝试过~因为还有涉及到后台的版块管理,表分开会容易点操作
呵呵,我也只是发表一下我的意见,仅供参考。如果你的小版块要有子版块的话,估计就需要一个表好一点。因为不可能在去新建一个表了是吧。
如;category表:id,fid,name,leftid(上一个版块),rightid(下一个版块),banzhu