明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

非遞歸法完成論壇樹型結(jié)構(gòu)及分頁(yè)!。ㄐ难Y(jié)晶啊,呵呵)

[摘要]現(xiàn)將本人的實(shí)踐結(jié)果show給大家,不足之處就是分頁(yè)的方法不太好,不能顯示具體的頁(yè)數(shù),可實(shí)在又沒(méi)有其它更好的解決辦法,只好先如此了,如果哪位有類似本論壇的分頁(yè)方法,表賜教一二,二泉不勝感激!具體可訪問(wèn)...
現(xiàn)將本人的實(shí)踐結(jié)果show給大家,不足之處就是分頁(yè)的方法不太好,不能顯示具體的頁(yè)數(shù),可實(shí)在又沒(méi)有其它更好的解決辦法,只好先如此了,如果哪位有類似本論壇的分頁(yè)方法,表賜教一二,二泉不勝感激!具體可訪問(wèn)我的個(gè)人小網(wǎng)站:http://web.nyist.net/~wbgwrq,不廢話了,開始吧......

//表的結(jié)構(gòu)如下:
//creat.sql
//簡(jiǎn)單說(shuō)明:RootId 論題序數(shù);Layer:帖子層次,縮進(jìn)的依據(jù);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

//發(fā)表根帖,即RootId,Layer,Orders為0的帖子
//said.php
//begin
<form method="post" action="post.php">
<table width="81%" border="0" cellspacing="1" cellpadding="0" align="center">
<tr bgcolor="#66CCCC">
<td height="35" width="23%">
<div align="right">你的想法:
</div>
</td>
<td height="35" width="77%">
<input type="text" name="title" size="50" class="eq">
</td>
</tr>
<tr bgcolor="#66CCCC">
<td height="39" width="23%">
<div align="right">你的內(nèi)容:
</div>
</td>
<td height="39" width="77%">
<textarea name="content" cols="50" rows="10" class="eq"></textarea>
</td>
</tr>
<tr bgcolor="#66CCCC">
<td colspan="2" height="24">
</td>
</tr>
<tr bgcolor="#66CCCC">
<td colspan="2" height="28">
<div align="center">
<input type="submit" name="said" value="先說(shuō)這些">
<input type="reset" name="Submit2" value="從頭再來(lái)">
</div>
</td>
</tr>

</form>
//said.php End


//帖子內(nèi)容,且在本頁(yè)進(jìn)行跟帖
//content.php
//Begin
<?php

$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");
?>
<table width="90%" cellspacing="1" bgcolor="red" align="center">
<tr bgcolor="#FFFFFF">
 <td width="12%">發(fā)表人:</td>
</tr>
<tr bgcolor="#FFFFFF">
 <td width="12%">主 題:</td>
 <td colspan="5">《<?php echo $title; ?>》<font color="666666"><i>【

Readed:<?echo"$readed";?>】<?echo"$date";?></i></font></td>
</tr>
 <tr bgcolor="#FFFFFF">
 <td width="12%">內(nèi) 容:</td>
 <td colspan="5"><?php echo $content; ?></td>
 </tr>

<br>
<center>
論壇發(fā)表跟帖
<form method="post" action="post.php">
<table width="78%" cellspacing="0" align="center" cellpadding="0" border="0">
<tr bgcolor="#FFFFFF">
<td height="24" width="12%">
<div align="right">題目:</div>
</td>
<td height="24" width="88%">
<input type="text" name="title" class="eq" size="50">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="63" width="12%">
<div align="right">內(nèi)容:</div>
</td>
<td height="63" width="88%">
<textarea name="content" class="eq" cols="50" rows="10"></textarea>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="57" colspan="2">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="20" colspan="2">
<div align="center">
<input type="submit" name="reply" value="跟帖" class=in>
<input type="reset" value="重寫" class=in name="reset">
<input type="hidden" name="rootid" value="<?echo $rootid;?>">
<input type="hidden" name="orders" value="<?echo$orders;?>">
<input type="hidden" name="layer" value="<?echo$layer;?>">
</div>
</td>
</tr>

//content.php End

//更新數(shù)據(jù)庫(kù)
//post.php
//Begin

<?
$content=nl2br(htmlspecialchars($content));
$title=htmlspecialchars($title);//決不允許在標(biāo)題上使用html
$date=date("Y-m-d H:i:s");
$length=strlen($content);

if(isset($said))//發(fā)表新帖子
{
$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))://發(fā)表跟帖

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

//重頭戲,顯示所有帖子,并實(shí)現(xiàn)分頁(yè)
//list.php
//Begin

<?php
//找到最新論題的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要根據(jù)$nextmaxid1變的
if($nextmaxid1>0) $maxid1=$nextmaxid1; //翻頁(yè)

//計(jì)算最小rootid:注意下面的desc,與limit結(jié)合,保證選取記錄的范圍.
//如果使用asc, 在mysql_result中檢索第0個(gè),將大大錯(cuò)誤!
$itemsperpage=30;
$query="select distinct rootid from over_post where rootid<=$maxid1 order by rootid desc limit

$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<=$maxid1 and rootid>=$minid1 order by rootid

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

}
else {
$minid1=0;
$maxid1=0;
echo "<p><center><font color='#ff0000'>沒(méi)有更多的發(fā)言內(nèi)容</font></center>";
}

$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<$minid1 order by rootid desc limit

$itemsperpage";
$r=mysql_query($query);
$n=mysql_num_rows($r);
if($n>0) $down=mysql_result($r,0);
else $down=$maxid1;
?>
<div align=center>
<br>
<table width="90%" border="0" cellspacing="1" cellpadding="0" align="center">
<tr bgcolor="2880AC">
<td height="20" align="center"><font color="#FFFFFF"><a

href="javascript:window.location.reload()" class=a1>刷新</a>
<a href="list.php" class=a1>首頁(yè)</a> <a href="list.php?nextmaxid1=<?echo$up;?>" class=a1>

上頁(yè)</a>
<a href="list.php?nextmaxid1=<?echo$down;?>" class=a1>下頁(yè)</a><?echo" 現(xiàn)存論題

:$startid1-$totalid1 ";?>
</font></td>
</tr>
<tr>
<tdheight="40" bgcolor="#E0F0F8" cellspacing="1">
<?php
echo"<ul>";
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="無(wú)內(nèi)容";
else $size.=" Bytes";
$rootid=$array["rootid"];
$orders=$array["orders"];
$layer=$array["layer"];

$ul="";//開始樹型結(jié)構(gòu)
$_ul="";
for($j=0;$j<$layer;$j++){
$ul=$ul."<ul>";
$_ul=$_ul."</ul>";
}
echo $ul."<li>"."<a href=\"content.php?id=$id\" class=big>$title</a><font color=006699>($size) 【

".作者."】 $postat <被讀:$readed> </font><br>".$_ul;
flush();
//樹型結(jié)構(gòu)結(jié)束
}
?><br></td>
</tr>
<tr bgcolor="2880AC">
<td height="20" align="center"><font color="#FFFFFF"><a

href="javascript:window.location.reload()" class=a1>刷新</a>
<a href="list.php" class=a1>首頁(yè)</a> <a href="list.php?nextmaxid1=<?echo$up;?>" class=a1>

上頁(yè)</a>
<a href="list.php?nextmaxid1=<?echo$down;?>" class=a1>下頁(yè)</a><?echo" 現(xiàn)存論題

:$startid1-$totalid1 ";?>
</font></td>
</tr>

</div>

//list.php End