web 中 bbs 例子(多次递归)
数据库设计:
create table `header`( // 父表
parent int not null, //父级
poster varchar(20) not null, //作者
posted
datetime, //时间
title varchar(100), //标题
children
tiny(1) default 0, //是否有回复(1)[YES] (0)[NO]
postid int not null primary key
// 主键
);
create table `body`( //具体内容
postid int not null primary
key,
text text
);
代码示例(省略头尾,只显示大概内容区):
<?php
/*
* expand_all() 全部展示
*
$expanded 标示那些已经展开的
* $expand 那些要被展开
*
$_GET['expand'] 表示那些是要被展示的
* $_GET['collapse']
表示那些是要折叠的
*/
session_start();
function
db_connect(){
$conn = new mysqli("localhost", "root", "123456",
"dbname");
$conn->query("set names utf8");
return $conn
}
//
全部要展示出来
function expand_all(&$expanded){
$sql = "select postid from
header where children = 1";
$conn = db_connect();
$result =
$conn->query($sql);
while($row = $result->fetch_assoc()){
$expanded[$row['postid']] = true;
}
}
// 调用函数 start开始的parent,
$row 标示行号,用于交替颜色
function display_tree($expanded, $row = 0, $start = 0){
echo "<table width='580'>\n";
$tree = new treenode($start, 1, true,
-1, $expanded);
$tree->display($row);
echo
"</table>\n";
}
if(!isset($_GET['expand'])){
$_SESSION['expanded'] = array();
}
//
那些是要显示出来的
if(isset($_GET['expand'])){
if($_GET['expand'] ==
'all'){
expand_all($_SESSION['expanded']);
}else{
$_SESSION[$_GET['expanded']] = true;
}
}
//
那些是要折叠
if(isset($_GET['collaspe'])){
if($_GET['collaspe'] ==
'all'){
$_SESSION['expanded'] = array();
}else{
unset($_SESSION[$_GET['collapse']]);
}
}
// class
class treenode
{
public $m_postid; // id
public $m_children; // 是否有回复
public $m_depth; // 深度,就是递归深入的深度
public $m_childlist; // 是一个数组,表示要展示的
id
function __construct($postid, $children, $expand, $depth,
$expanded){
$this->m_postid = $postid;
$this->m_children =
$chilren;
$this->m_depth = $depth;
$this->m_childlist =
array();
if($expand && $this->m_children){
$conn =
db_connect();
$query = "select * from header where parent = '" .
$this->postid . "' order by posted";
$result =
$conn->query($query);
for($count=0; $row = $conn->fetch_assoc();
$count++){
if($expanded[$row['postid']] == true){
$expand =
true;
}else{
$expand = false;
}
$this->m_childlist[$count] = new treenode($row['postid'],
$row['children'],$expand, $depth+1, $expanded);
}
}
}
function display($row){
if($this->depth>-1){
echo
"<tr><td bgcolor=\"";
if($row%2){
echo
"#cccccc\">";
}else{
echo "#ffffff\">"
}
// 缩进
for($i=0; $i<$this->depth; $i++){
echo
"  p;";
}
if($this->children &&
sizeof($this->m_childlist)){
echo "<a href=\"index.php?collapse=\""
. $this->postid . "\"><img
src=\"images/--.gif\"/></a>";
}else
if($this->children){
echo "<a href=\"index.php?expand=\"" .
$this->postid . "\"><img
src=\"images/++.gif\"/></a>";
}else{
echo
" ";
}
echo "<a
href=\"view.php?parent={$this->postid}\">" . $this->postid
."</a></td></tr>\n";
$row++;
}
$num_children = sizeof($this->m_childlist);
for($i=0;
$i<$num_children; $i++){
$row =
$this->m_childlist[$i]->display($row);
}
return $row;
}
}
//
最后调用
display_tree($_SESSION['expanded']);
?>
图片效果如下:
首页初始化时如下:

全部展开时如下

以下是部分展开的两个效果图:


web 中 bbs 例子(多次递归)的更多相关文章
- HT for Web中3D流动效果的实现与应用
流动效果在3D领域有着广泛的应用场景,如上图中医学领域可通过3D的流动直观的观察人体血液的流动,燃气领域可用于监控管道内流动的液体或气体的流向.流速和温度等指标. 如今企业数据中心机房普遍面临着设备散 ...
- 基于gSOAP使用头文件的C语言版web service开发过程例子
基于gSOAP使用头文件的C语言版web service开发过程例子 一服务端 1 打开VS2005,创建一个工程,命名为calcServer. 2 添加一个头文件calc.h,编辑内容如下: 1// ...
- 【spring实战第五版遇到的坑】3.1中的例子报错
按照书中的例子,一直做到第3.1章使用JDBC读写数据时,在提交设计的taco表单时,报了如下的异常信息: Failed to convert property value of type java. ...
- Web中的积累:外观模式 Facade
摘要: 原创出处: http://www.cnblogs.com/Alandre/ 泥沙砖瓦浆木匠 希望转载,保留摘要,谢谢! 壹 前言 目测好久没写文章了,距离上一篇文章也有二十多天.我是怎么了?哈 ...
- Html的本质及在web中的作用
概要 本文以一个Socket程序为例由浅及深地揭示了Html的本质问题,同时介绍了作为web开发者我们在开发网站时需要做的事情 Html的本质以及开发需要的工作 1.服务器-客户端模型 其实,对于所有 ...
- [翻译]Spring MVC RESTFul Web Service CRUD 例子
Spring MVC RESTFul Web Service CRUD 例子 本文主要翻译自:http://memorynotfound.com/spring-mvc-restful-web-serv ...
- C#中的函数式编程:递归与纯函数(二) 学习ASP.NET Core Razor 编程系列四——Asp.Net Core Razor列表模板页面
C#中的函数式编程:递归与纯函数(二) 在序言中,我们提到函数式编程的两大特征:无副作用.函数是第一公民.现在,我们先来深入第一个特征:无副作用. 无副作用是通过引用透明(Referential ...
- java web中各种context的关系
我举得这篇文章解决了我的很多疑惑,理清了我以前不太清楚的Context关系,读懂这篇文章很有助于理解源码, 原文链接在这里:https://www.jianshu.com/p/2537e2fec546 ...
- 优化Web中的性能
优化Web中的性能 简介 web的优化就是一场阻止http请求最终访问到数据库的战争. 优化的方式就是加缓存,在各个节点加缓存. web请求的流程及节点 熟悉流程及节点,才能定位性能的问题.而且优化的 ...
随机推荐
- C#- WinForm获取 当前执行程序路径的几种方法
1.获取和设置当前目录的完全限定路径.string str = System.Environment.CurrentDirectory;Result: C:xxxxxx 2.获取启动了应用程序的可执行 ...
- 递归小demo(1-100的和)
public class demo1 { public static void main(String[] args) { //初始值为100 int n = 100; //调用number方法,返回 ...
- jQuery EasyUI, datagrid, treegrid formatter 参数比较 row index
如题: datagrid中,见官方文档: formatter function The cell formatter function, take three parameter:value: the ...
- Integer Inquiry_hdu_1047(大数).java
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 标准I/O库之标准输入、标准输出和标准出错
对一个进程预定义了三个流,并且这三个流可以自动地被进程使用,它们是:标准输入.标准输出和标注出错.这些流引用的文件与文件描述符STDIN_FILENO.STDOUT_FILENO和STDERR_FIL ...
- [Form Builder]内置函数execute_trigger、do_key详解
转:http://yedward.net/?id=82 1.execute_trigger:用来运行一个指定的触发器,常用来运行用户自定义的触发器. 语法:procedure execute_trig ...
- wins和linux 系统不同编码格式导致的.py执行问题: bad interpreter: No such or file directory
我在win7上用IDLE编写了一个python文件(MyTopo.py),但是用putty传到VM中的ubuntu系统中,用 ./MyTopo方式执行. 显示: /bin/sh^M: bad inte ...
- Bootstrap的竞争对手Zurb Foundation
Bootstrap并不是唯一的前端开发框架,比如还有JQuery UI.HTML5 Boilerplate等等.但对于Bootstrap来说,真正的竞争对手是Zurb Foundation.Boots ...
- java_method_删除事务回滚
public String[] deleteEPGroup(String groupID, String groupName) { String[] operRes=new String[3]; if ...
- hibernate篇章一
我只想说一句话FUCK! 昨晚查了一晚上资料. 今早细致勃勃的准备搭建环境,早上到现在失败!失败!失败!蛋疼 有兴趣的可以试试.我暂时搁置,去做分页了 有兴趣需要相关资料的可以查阅我的微博java66 ...