Home  >  Article  >  php教程  >  非递归法实现论坛树型结构及分页

非递归法实现论坛树型结构及分页

WBOY
WBOYOriginal
2016-06-21 09:05:231303browse

递归|分页|树型结构

现将本人的实践结果show给大家,不足之处就是分页的方法不太好,不能显示具体的页数,可实在又没有其它更好的解决办法,只好先如此了,如果哪位有类似本论坛的分页方法,表赐教一二,二泉不胜感激!具体可访问我的个人小网站:http://web.nyist.net/~wbgwrq,不废话了,开始吧......

//表的结构如下:
//creat.sql
//简单说明:rootid 论题序数;layer:帖子层次,缩进的依据;orders:帖子的顺序
create table over_post (
id int(11) not null auto_increment,
title varchar(80) not null default '',
content text,
postat datetime not null default '0000-00-00 00:00:00',
readed int(11) not null default '0',
size int(11) not null default '0',
rootid int(11) not null default '0',
orders int(4) not null default '0',
layer int(4) not null default '0',
primary key (id)
) type=myisam;
//creat.sql end

//发表根帖,即rootid,layer,orders为0的帖子
//said.php
//begin


















你的想法:




你的内容:











//said.php end


//帖子内容,且在本页进行跟帖
//content.php
//begin

$result=mysql_query("select

over_post.title,over_post.content,over_post.postat,over_post.readed,over_post.rootid,over_post.la

yer,over_post.orders from over_post where over_post.id=$id");
$readed=mysql_result($result,0,"readed");
$title=mysql_result($result,0,"title");
$content=mysql_result($result,0,"content");
$date=mysql_result($result,0,"postat");
$rootid=mysql_result($result,0,"rootid");
$orders=mysql_result($result,0,"orders");
$layer=mysql_result($result,0,"layer");
?>



 


 
 

 
 
 
 
发表人:
主 题:

readed:

内 容:




论坛发表跟帖

















题目:




内容:













//content.php end

//更新数据库
//post.php
//begin


$content=nl2br(htmlspecialchars($content));
$title=htmlspecialchars($title); //决不允许在标题上使用html
$date=date("y-m-d h:i:s");
$length=strlen($content);

if(isset($said)) //发表新帖子
{
$query="insert into over_post

values(null,'$title','$content',$user_id,'$date',0,$length,$img,'','','')";
$result=mysql_query($query) or die(mysql_error());
$r=mysql_query("select max(id) from over_post");
$rootid = mysql_result($r,0)+0;
mysql_query("update over_post set rootid=$rootid where id=$rootid")or die(mysql_error());
}

if(isset($reply)): //发表跟帖

mysql_query("update over_post set orders=orders+1 where rootid=$rootid and orders>$orders")or

die(mysql_error());

$layer=$layer+1;
$orders=$orders+1;
$query="insert into over_post

values(null,'$title','$content',$user_id,'$date',0,$length,$img,$rootid,$orders,$layer)";

$result=mysql_query($query) or die(mysql_error());

endif;
 if($result) {
include"list.php";
}
?>
//post.php end

//重头戏,显示所有帖子,并实现分页
//list.php
//begin

//找到最新论题的rootid
$query = "select max(rootid) as maxid1, min(rootid) as minid1 from over_post";
$result = mysql_query($query);
$maxid1 = mysql_result($result, 0, "maxid1");
$startid1 = mysql_result($result, 0, "minid1");
if(!($maxid1>0)) $maxid1=0;
if(!($startid1>0)) $startid1=0;
$totalid1 = $maxid1; //这是真正的最大的rootid值, $maxid1要根据$nextmaxid1变的
if($nextmaxid1>0) $maxid1=$nextmaxid1; //翻页

//计算最小rootid:注意下面的desc,与limit结合,保证选取记录的范围.
//如果使用asc, 在mysql_result中检索第0个,将大大错误!
$itemsperpage=30;
$query="select distinct rootid from over_post where rootid

$itemsperpage";
$r=mysql_query($query);
$n=mysql_num_rows($r);
if($n>0) {
$minid1=mysql_result($r,$n-1);
$query="select * from over_post where rootid=$minid1 order by rootid

desc,orders";
$result=mysql_query($query);
$num=mysql_num_rows($result);

}
else {
$minid1=0;
$maxid1=0;
echo "

没有更多的发言内容
";
}

$query="select distinct rootid from over_post where rootid>$maxid1 order by rootid limit

$itemsperpage";
$r=mysql_query($query);
$n=mysql_num_rows($r);
if($n>0) $up=mysql_result($r,$n-1);
else $up=$totalid1;

$query="select distinct rootid from over_post where rootid

$itemsperpage";
$r=mysql_query($query);
$n=mysql_num_rows($r);
if($n>0) $down=mysql_result($r,0);
else $down=$maxid1;
?>














href="javascript:window.location.reload()" class=a1>刷新


首页

上页


下页

:$startid1-$totalid1 ";?>


echo"
    ";
    while ($array=mysql_fetch_array($result)){
    $id=$array["id"];
    $title=$array["title"];
    $content=$array["content"];
    $postat=$array["postat"];
    $readed=$array["readed"];
    $size=$array["size"];
    if($size==0) $size="无内容";
    else $size.=" bytes";
    $rootid=$array["rootid"];
    $orders=$array["orders"];
    $layer=$array["layer"];

    $ul=""; //开始树型结构
    $_ul="";
    for($j=0;$j$ul=$ul."

      ";
      $_ul=$_ul."
    ";
    }
    echo $ul."
  • "."$title($size) 【

    ".作者."】 $postat


    ".$_ul;
    flush();
    //树型结构结束
    }
    ?>

href="javascript:window.location.reload()" class=a1>刷新


首页

上页


下页

:$startid1-$totalid1 ";?>


//list.php end



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn