简单实用的框架式树型论坛代码<二>
效果请参考:http://teaman.oso.com.cn/teabbs/
共三个页面 index.html ,list.php, teamanbbs.php 
 <?php
//这是用来发表留言、回复帖子、显示帖子等的页面 teamanbbs.php
 
function data_format($date) {//对留言、主题等进行处理的函数
  
$fdate trim($date);
  
$fdate addslashes($fdate);
  return 
$fdate;
}

 
mysql_pconnect("localhost","teaman","sex") OR die ("不能连接数据库");
 @
mysql_select_db("teaman") or die ("连接数据库失败");

 function 
showmote() { //留言表情的显示
$allmote=14;
echo 
'留言表情<br>';
echo 
'<ul>';
for (
$i=1;$i<=$allmote;$i++) {
echo 
"<input type='radio' name='mote' value='".$i."'><img src='img/mood".$i.".gif' width='20' height='20'> ";
if (
$i%7==0) echo "<br>"//到第7个和14个时就起一行
  
}
}

 function 
listmsg($parent,$off,$lim) {     //这里用来调用相关帖子的子帖
global $theid,$php_self,$page;
$sql="select id,pid,author,title,date,msg,mote,rec,rid from teamanbbs where pid=$parent order by id limit $off,$lim";
$result=mysql_query($sql);
echo 
"<ul>";

while (list(
$id,$pid,$author,$title,$date,$msg,$mote,$rec,$rid) = mysql_fetch_row($result)) {
$author=stripslashes($author);//本来这些都应该调用函数来实现比较方便,懒改了
  
$author=htmlspecialchars($author);
  
$title=stripslashes($title);
  
$title=htmlspecialchars($title);

echo 
"<li><img src='img/mood".$mote.".gif' width='20' height='20'>";
  if (
$id!=$theid) echo "<a href='teamanbbs.php?cid=show&page=".$page."&theid=".$id."' target='teamanbbs'>"; else echo "<b>"//如果相关文章当前的帖子是本身,就加粗,否则就链接
  
echo $title;
  if (
$id!=$theid) echo "</a>"; else echo "</b>"
 if(
$email!="") echo "<a href='mailto:".$email."'>";
 echo 
"<b>---".$author."&nbsp;</b>";
 if (
$email!="") echo "</a>";
 echo 
$date."[点击:".$rid."]";
 if (
$msg=="") echo "(无内容)"; else echo "(".strlen($msg)."字)</li>";
   
  if (
$rec>0listmsg($id,0,20);//看是否还有跟帖,发帖的时候判断是跟还是新,
  
}
  echo 
"</ur>";
}
function 
listmsg2($id,$off,$lim) {    //显示相关帖子用
global $theid,$php_self;
$sql="select id,pid,date,rec,author,email,title,mote from teamanbbs where id=$id and pid=0 order by id desc limit $off,$lim";  //此句限制只取一条
$result=mysql_query($sql);
  echo 
"<ul>";
  while (list(
$id,$pid,$date,$rec,$author,$email,$title,$mote)= mysql_fetch_row($result)) {
  
$author=stripslashes($author);
  
$author=htmlspecialchars($author);
  
$title=stripslashes($title);
  
$title=htmlspecialchars($title);
   echo 
"<li><img src='img/mood".$mote.".gif' width='20' height='20'>";
   if (
$id!=$theid ) echo  "<a href='".$php_self."?cid=detail&theid=".$id."'>"; else echo "<b>";
   echo 
$title;
   if (
$id!=$theid) echo "</a>";else echo "</b>";
   if (
$email!="") echo "<a href='mailto:".$email."'>"; else echo "<b>";
   echo 
"---".$author;
   if (
$email!="") echo "</a>"; else echo "</b>";
   echo 
$date."[点击:".$rid."]";
 if (
$msg=="") echo "(无内容)"; else echo "(".strlen($msg)."字)</li>";
 
  if (
$rec>0listmsg($id,0,20);//看是否还有跟帖,发帖的时候判断是跟还是新
  
}
  echo 
"</ur>";
}

function 
showmsg($theid) {       //显示留言详细情况的函数
global $PHP_SELF,$page,$_email,$homepage;
$sql="update teamanbbs set clicks=clicks+1 where id=$theid";
$result=mysql_query($sql);
$sql="select id,pid,author,title,homepage,date,msg,mote,rec,rid,clicks from teamanbbs where id=$theid";
$result=mysql_query($sql);
if (list(
$id,$pid,$author,$title,$homepage,$date,$msg,$mote,$rec,$rid,$clicks)=mysql_fetch_row($result)) {
$author stripslashes($author);
$titlestripslashes($title);
$title=htmlspecialchars($title);
$msghtmlspecialchars($msg);
$msg nl2br($msg);
$msg =str_replace(" ","&nbsp;",$msg);

?> 
<link rel="stylesheet" href="teamancss/bbs.css">
<style type="text/css">
<!--
body {  background-attachment: fixed; background-image: url(img/back.gif); font-family: "Arial", "Helvetica", "sans-serif", "Arial Narrow"; font-size: 9pt}
-->
</style>

<body class="p9">
<table border="0" cellpadding="0" cellspacing="0" class="p9" width="760">
  <tr><td colspan="2" align="left" valign="middle" >
            <?php
                
echo '作者:'.$author.' 时间:'.$date;
                if (
$email!='') echo ' <a href="mailto:'.$email.'">邮件</a>';
                if (
$homepage!='http://') echo ' <a href="'.$homepage.'" target=_blank>主页</a>';
                echo 
' 回复数:'.$rec;
                echo 
'查看次数:'.$clicks;
            
?>
          </td></tr>
          <tr>
    <td colspan="2" align="left" valign="top" height="455" class="p9"> <span class="p9"><?php
          
echo '<center><b>'.$title.'</b></center>'.$msg.'<hr size=1>相关文章:';
          
$rid=($pid==0)?$id:$rid;  
          
listmsg2($rid,0,20);  //调用显示相关文章
          
echo '<hr size=1>回复文章:(带 <font color=red>*</font> 号为必填)';
          
$title=str_replace('<无内容>','',$title);
          
?></span> 
      <form name="form1" method="post" action="<?php echo $PHP_SELF?>" onSubmit="return checkdata()">
          姓名:
        <INPUT TYPE="text" NAME="author"  maxlength="100" class="teabox2" >
         <font color=red>*</font><br>
          邮件:
        <INPUT TYPE="text" NAME="email"  maxlength="100" class="teabox2" ">
        <br>
          主页:
        <INPUT TYPE="text" NAME="homepage"  size="40" value="http://" maxlength="200" class="teabox2">
      <br>
          主题:
        <INPUT TYPE="text" NAME="title"  size="40" value="回复:<?php echo $title?>" maxlength="200" class="teabox2">
       <font color=red>*</font><br>
      <?php showmote(); ?> 内容(最多16K):
        <input type="submit" name="submit2" value=" 发 送 " class="teabox1" >
        <input type="reset" value=" 清 除 " name="reset" class="teabox1" >
      <br>
          <TEXTAREA NAME="msg" ROWS="8" COLS="65" class="box2"></TEXTAREA><br>
      <INPUT TYPE="hidden" name="cid" value="post"><INPUT TYPE="hidden" name="p" value="<?php echo $id?>"><INPUT TYPE="hidden" name="r" value="<?php echo $rid?>"><INPUT TYPE="hidden" name="page" value="<?php echo $page?>">
          </form>
          </td></tr></table>
<span class="p9"></span> <span class="p9"></span><?php
      
        
}
   }
 function 
newmsg() {
    global 
$PHP_SELF,$_user,$page,$_email,$_homepage;
      
?> 
<tr class="p9"> 
  <td colspan="2" align="left" valign="top" height="475" class="p9"> 发表文章:(带 <font color=red>*</font> 
    号为必填)<span class="p9"></span><br>
      
    <form name="form1" method="post" action="<?php echo $PHP_SELF?>" onSubmit="return checkdata()" class="p9">
      姓名:
      <INPUT TYPE="text" NAME="author"  maxlength="100" class="teabox2" >
       <font color=red>*</font><br>
      邮件:
      <INPUT TYPE="text" NAME="email"  maxlength="100" class="teabox2" >
      <br>
      主页:
      <INPUT TYPE="text" NAME="homepage"  size="60" value="http://" maxlength="200" class="teabox2">
      <br>
      主题:
      <INPUT TYPE="text" NAME="title"  size="60" value="" maxlength="200" class="teabox2">
      <font color=red>*</font><span class="p9"></span><br>
      <?php showmote(); ?> 内容(最多16K):
      <input type="submit" name="submit" value=" 发 送 " class="teabox1" >
      <span class="p9">
      <input type="reset" value=" 清 除 " class="teabox1" name="reset2">
      </span> <br>
      <TEXTAREA NAME="msg" ROWS="8" COLS="65" ></TEXTAREA><br>
      <INPUT TYPE="hidden" name="cid" value="post"><INPUT TYPE="hidden" name="p" value="0"><INPUT TYPE="hidden" name="r" value="0"><INPUT TYPE="hidden" name="page" value="<?php echo $page?>">
      </form>
      </td></tr><?php
  
}
function 
postmsg() { // 发表留言

      
    
global $author,$email,$homepage,$title,$p,$r,$msg,$mote,$REMOTE_ADDR;
    
$author data_format($author);
    
$email data_format($email);
    
$homepage data_format($homepage);
    
$title data_format($title);
    
$pid = ($p=='')?0:$p;
    
$rid = ($r=='')?0:$r;
    if (
$msg==''$title .= '<无内容>';
    if (
$mote==''$mote=1;
    
$date date("Y-m-d H:i:s");
    
$sql "Insert into teamanbbs(pid,rid,date,rec,author,email,homepage,title,msg,ip,mote) values($pid,$rid,'$date',0,'$author','$email','$homepage','$title','$msg','$REMOTE_ADDR',$mote)";
    
$result mysql_query($sql);
    
?><tr><td colspan="2" align="left" valign="top" height="475"><?php
    
if ($result) {
      echo 
'您发表的文章如下:';
      echo 
'<br>作者:'.$author;
      if (
$email!='') echo '<br>邮件:<a href="mailto:'.$email.'">'.$email.'</a>';
      if (
$homepage!='http://') echo '<br>主页:<a href="'.$homepage.'" target=_blank>'.$homepage.'</a>';
      echo 
'<br>主题:'.$title;
      if (
$msg!='') {
        
$msg=htmlspecialchars($msg);
        
$msg=nl2br($msg);
        
$msg=str_replace(' ','&nbsp;',$msg);
        echo 
'<br>正文:<br><pre>'.$msg.'</pre>';
      }
      
?><p><?php
      $sql 
"update teamanbbs set rec=rec+1 where id=$pid";
      
$result mysql_query($sql);
    } else {
      echo 
'<h3>发表失败了!</h3>';
    }
    
//echo $sql;
    
?></td></tr><?php
  
}
?>


<html>
<head>
<title>teamanbbs</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="teamancss/bbs.css">
<style type="text/css">
<!--
body {  background-attachment: fixed; background-image: url(img/back.gif); font-family: "Arial", "Helvetica", "sans-serif", "Arial Narrow"; font-size: 9pt}
-->
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
function checkdata() {
  if (form1.author.value=='' || form1.title.value=='') {
    alert("带 * 号的项目必须填写!")
    if (form1.author.value=='') form1.author.focus();
    else form1.title.focus();
    return false
  }
}

//-->
</SCRIPT>
</head>

<body bgcolor="#FFFFFF">
<?php
if ($cid=="addnew") { //根据$cid判断是否为新加帖子
newmsg(); //就调用新帖子函数
} elseif ($cid=="show") {//判断是否显示帖子
 
showmsg($theid); //就调用显示帖子
} elseif ($cid=="post")  { //判断是否发送新帖或回复帖子
  
postmsg();  //就调用发送并存到数据库的函数
} else { //如果上面都不是,也就是第一次进去,那当然是给个欢迎或说明的页面
 
echo "<p>见过很多论坛和留言版,觉得不是太复杂了就是太简单......</p>";
}
 
?>
<center><a href="http://teaman.top263.net" target="_blank">Home of teaman</a> 2001.1.7 </center>

</body>
</html>

参考文献:
1.漫步精品的论坛: http://myjingpin.com
2.用PHP+MYSQL实现论坛里的分级+分页显示:http://www.oso.com.cn/read_article.php?article_id=207