1. 程式人生 > >PHP+HTML簡單實現BBS論壇與回帖

PHP+HTML簡單實現BBS論壇與回帖

        其中main_right3-1-content.php程式碼如下:
<?php
	header("Content-Type: text/html; charset=utf-8");
	include ("../database/human.php");
	session_start(); 
?>
<link rel="stylesheet" href="css/main.css" type="text/css"  />
<TABLE cellSpacing=0 cellPadding=0 width="100%" align=center border=0>
  <TR height=28>
    <TD background=images/title_bg1.jpg>當前位置: BBS論壇</TD>
  	<TD align=right><A href="main_right6-1.php" class="one"> 返回 </A></TD>  
  </TR>
  <TR>
    <TD bgColor=#b1ceef height=1 colspan="2"></TD></TR>
  <TR height=10>
    <TD background=images/shadow_bg.jpg colspan="2"></TD></TR>
</TABLE>
<!-- PHP資料庫中獲取內容 -->
<?php
	//查詢BBS
	$hgi=new HttpPostInf();
	$result=$hgi->doquery('2',"select * from BBS_Post where BP_Pid='".$_GET['bbsid']."';"); 
	//解析json
	$dj=json_decode($result,true);
	$jcount=count($dj);
	for($i=0; $i<$jcount; $i++){	
		$_SESSION['bbsid']=$dj[$i]['BP_Pid'];
?>
<div class="middle">
<div class="wrap">
	<div class="wrap_left">
			<div class="wenda-head">
        <TABLE cellSpacing=0 cellPadding=0 width="100%" align=center border=0 bgcolor="blue">
				  <TR height=10>
				    <TD></TD></TR>
				  <TR height=22>
				    <TD style="PADDING-LEFT: 10px; FONT-WEIGHT: bold; COLOR: #ffffff" 
				    	align=left>[貼] <?php echo $dj[$i]['BP_Ptitle']; ?></TD></TR>
				  <TR height=10>
				    <TD></TD></TR>
				</TABLE>    
    	</div>
    <!-- 樓主內容 -->
     <TABLE cellSpacing=0 cellPadding=0 width="95%" align=center border=0>
			  <tr>
			    <td valign="top"><div align="middle" width=150><BR /><BR /><img 
			    	src='images/tx.jpg' width='60' height='60' />
			    	<BR /><BR />樓主 1# <BR />
			    	<?php echo $dj[$i]['BP_Puserid']; ?>
			   			<BR />身份
			    		<?php 
                if($dj[$i]['BP_Ptype']==0) echo "教師";
                if($dj[$i]['BP_Ptype']==1) echo "學生";
               ?><BR /><?php echo $dj[$i]['BP_Ptime']; ?></div></td>
			    <!-- 多行文字輸入控制元件 disabled="disabled"不可編輯 -->
			    <td ><textarea rows="15" type="text" width="400px" style="resize:none;font-size:16px;" 
			    	maxlength="2000" name="content" cols="60" disabled="disabled"><?php echo $dj[$i]['BP_Cont']; ?></textarea></td>
			  </tr>
			  <TR height=4>
			    <TD colspan="3"></TD>
			  </TR>
			  <tr>
			    <td colspan="2"><hr width="100%" size="2" color="#FF0000"></td>
			  </tr>
			</TABLE>
			<!--上面的php中while迴圈並沒有結束,還有右括號-->
			<?php
				}
				//查詢BBS
				$hgi=new HttpPostInf();
				$result=$hgi->doquery('2',"select * from BBS_Reply where BR_Pid='".$_GET['bbsid']."';"); 
				//解析json
				$dj=json_decode($result,true);
				$jcount=count($dj);
				for($i=0; $i<$jcount; $i++){	
			?>
			<!-- 樓層回覆內容 -->
			<TABLE cellSpacing=0 cellPadding=0 width="95%" align=center border=0>
			  <tr>
			    <td valign="top"><div align="middle" width=150><BR /><BR />
			    	<img src='images/tx.jpg' width='60' height='60' />
			    	<BR /><BR />樓層 <?php $_SESSION['bbshfid']=time(); echo $i+2;?># <BR />
			    	<?php echo $dj[$i]['BR_Ruserid']; ?>
			   			<BR />身份
			    		<?php 
                if($dj[$i]['BR_Rtype']==0) echo "教師";
                if($dj[$i]['BR_Rtype']==1) echo "學生";
               ?>
        			 <BR /><?php echo $dj[$i]['BR_Ptime']; ?>
               <BR />回覆樓層<?php echo $dj[$i]['BR_Pfloor']; ?>#</div></td>
			    <!-- 多行文字輸入控制元件 disabled="disabled"不可編輯 -->
			    <td ><textarea rows="15" type="text" width="400px" style="resize:none;font-size:16px;" 
			    	maxlength="2000" name="content" cols="60" disabled="disabled"><?php echo $dj[$i]['BR_Cont']; ?></textarea></td>
			  </tr>
			  <TR height=4>
			    <TD colspan="3">
			    </TD>
			  </TR>
			  <tr>
			    <td colspan="2"><hr width="100%" size="2" color="#FF0000"></td>
			  </tr>
			</TABLE>
			<?php
				}
			?>
			<!-- 回覆內容 -->
			<form id="form1" name="form1" method="post" action="main_right3-1-content.php">
			<TABLE cellSpacing=0 cellPadding=0 width="95%" align=center border=0>
			  <tr>
			  	<td colspan="2">  回覆樓層
			  		<input type="text" name="lc" id="lc" value="" style=width:150pt; maxlength="50"/>
			  	</td>
			  </tr>
			  <tr>
			    <td valign="top"><div align="middle" width=150>
			    	<BR /><BR />   回覆   <BR /></div></td>
			    	<!-- 多行文字輸入控制元件 disabled="disabled"不可編輯 -->
			    <td ><textarea rows="10" type="text" width="400px" style="resize:none;font-size:16px;" maxlength="2000" 
			    	name="bbscontent" cols="60" ></textarea></td>
			  </tr>
			  <tr>
			  	<td colspan="2" align="middle">
			  		<input type="submit" style='font-size:22px' name="Submit" value="提交回復"/>
			  	</td>
			  </tr>
		  	<TR height=4><TD colspan="3"></TD></TR>
			  <tr>
			    <td colspan="2"><hr width="100%" size="2" color="#FF0000"></td>
			  </tr>
			</TABLE>
     	</form>
</div><!--wrap-->
</div><!--wrap_right-->    
</div><!--middle-->
<TABLE cellSpacing=0 cellPadding=2 width="95%" align=center border=0>
	<TR height=20>
    <TD></TD></TR>
</TABLE>

<!-- 提交當前頁面POST資料庫表單處理 -->
<?php
	if (!empty($_POST['bbscontent'])) 
	{
		if($_SESSION['radio']==1) $a=1; //學生
		if($_SESSION['radio']==2) $a=0; //老師
		//SQL更新
		$sql = "INSERT INTO BBS_Reply (BR_Rid,BR_Pid,BR_Ruserid,BR_Rtype,BR_Ptime,BR_Pfloor,BR_Cont) VALUES (";
		$sql .= "'".$_SESSION['bbshfid']."',";
		$sql .= "'".$_SESSION['bbsid']."',";
		$sql .= "'".$_SESSION['number']."',";
		$sql .= "'".$a."',";
		$sql .= "'".date('Y-m-d')."',";
		$sql .= "'".$_POST['lc']."',";
		$sql .= "'".$_POST['bbscontent']."'";
		$sql .= ");";	
		//更新資訊
		//echo $sql;
		$hgi=new HttpPostInf();
		$result=$hgi->doquery('1',$sql);
		//echo "<script>alert('恭喜你修改成功!');</script>";
		header('Location:main_right3-1.php');
	}
?>
        發帖回覆顯示效果如下圖所示:

        發帖的程式碼就不貼出來了,可以檢視原始碼。寫到此處可能你會疑惑資料庫訪問部分的內容,下面進行簡單講解吧!