PHP+HTML simple realization of BBS forum and reply

        This article mainly describes how to simply implement the BBS forum and posting/replying functions through PHP+HTML. This is to extract part of the content of our php project. The main content includes:
        1. Realize the partial layout interface through JavaScript and Iframe
        2. How to define the PHP class to realize the database access function
        3. Realize the simple BBS forum and post/reply function.
        Since this project was completed by ten people in the winter vacation, SAE was adopted. Build an online back-end database, and other people use Apache to design web pages and access the database locally, which is equivalent to simple BS three-terminal access.
        Source code download address: http://download.csdn.net/detail/eastmount/8501443
        Visit http://localhost:8080/testphp/index.php and the effect is shown in the following figure:



 

1. Home page layout construction

        The first is to build the layout of the home page, the main layout is divided into head, main_left and main_right. The display on the right uses the partial display realized by iframe, while the left uses JavaScript to realize the click hide and display functions. The home page code of index.php is as follows:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>《分布式系统》精品课程学习</title><link  href="css/mycss.css" type="text/css" rel="stylesheet"/><style type="text/css">	/* 这个链接改变颜色 */	a.one:link {color: #ff0000}	a.one:visited {color: #0000ff}	a.one:hover {color: #ffcc00}</style></head><body>	<!-- 布局顶部登录状态 --><div id="main"><TABLE cellSpacing=0 cellPadding=0 width="100%" 		background="images/header_bg.gif" border=0>	<!--头部图片--><TR height=80><TD width=260>    	<IMG height=80 src="images/logo.png" width=260></TD><TD style="FONT-SIZE: 12px; FONT-WEIGHT: bold; COLOR: #000;    	PADDING-TOP: 20px; PADDING-RIGHT: 20px" align=right>	  	您还未登录! |	<A class="one" href="login.php" >登录</A>|	<A class="one" href="register_student.php" >学生注册</A>|	<A class="one" href="register_teacher.php" >教师注册</A> 	</TD></TR></TABLE><TABLE cellSpacing=0 cellPadding=0 width="100%" border=0><TR bgColor=#1c5db6 height=4><TD></TD></TR></TABLE><br /><!-- 布局中部 --><div id="middle"><!-- 布局中部右边 -->	<div id="index_right"><iframe height="100%" width="100%" border="0" frameborder="0" 	src="main_right.php" name="rightFrame" id="rightFrame" title="rightFrame"></iframe></div><!- The left side of the middle of the layout --><div id="index_left"><?php include('main_left.php');?></div></div>

        The CSS code layout here calls the mycss.css implementation in the css file:

html, body {height: '100%';width: '100%';}/*头部布局*/#main{	margin-left:200px;	width:950px;}/*中部布局*/#middle{	width:950px;	height:500px;	background-color:#b6effa;}/*中部左边*/#index_left{	float:left;	margin-top:10px;	margin-left:10px;	width:180px;	height:480px;}/*中部右边*/#index_right{	float:right;	margin-top:10px;	margin-right:20px;	width:720px;	height:480px;	border-style:double;}/*中部右边*/#logout_middle{	float:right;	margin-top:100px;	margin-right:220px;	width:500px;	height:300px;	border-style:double;	background-color:#ffffff;}

        The main code in Iframe is <iframe src="main_right.php" name="rightFrame" id="rightFrame">, when calling "<A href="main_right3-1.php" target="rightFrame">BBS forum </A>" Just declare the target as rightFrame. At the same time hide and display, set style.display to block or none.
        The main_left.php code is as follows, and the above layout can be realized at this time. 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><META http-equiv=Content-Type content="text/html; charset=utf-8"><LINK href="css/admin.css" type="text/css" rel="stylesheet"><SCRIPT language=javascript>	function expand(el) {		childObj = document.getElementById("child" + el);		if (childObj.style.display == 'none') {			childObj.style.display = 'block';		} else {			childObj.style.display = 'none';		}		return;	}</SCRIPT></HEAD><BODY><TABLE height="100%" cellSpacing=0 cellPadding=0 width=170 background=images/menu_bg.jpg border=0 align=left><TR><TD vAlign=top align=middle><TABLE cellSpacing=0 cellPadding=0 width="100%" border=0><TR><TD height=10></TD></TR></TABLE>			<!-- 第一选项 -->			<TABLE cellSpacing=0 cellPadding=0 width=150 border=0>  			<TR height=30>			<TD style="PADDING-LEFT: 20px; FONT-SIZE: 15px" background=images/menu_bt.jpg>			    	<A class=menuParent οnclick=expand(1) 			      	href="javascript:void(0);">课程首页</A></TD></TR>			<TR height=4>			<TD></TD></TR>			</TABLE>			<TABLE id=child1 style="DISPLAY: none" cellSpacing=0 cellPadding=0 			width=150 border=0>			<TR height=20>			<TD align=middle width=30><IMG height=9 			src="images/menu_icon.gif" width=9></TD>			<TD><A href="main_right1-1.php" 			    	target="rightFrame">首页介绍</A></TD></TR>			<TR height=4>			<TD colSpan=2></TD></TR>			</TABLE>			<!-- 第二选项 --><TABLE cellSpacing=0 cellPadding=0 width=150 border=0><TR height=30><TD style="PADDING-LEFT: 20px; FONT-SIZE: 15px" background=images/menu_bt.jpg>          	<A class=menuParent οnclick=expand(2)             	href="javascript:void(0);">课程概况</A></TD></TR><TR height=4><TD></TD></TR></TABLE>			<TABLE id=child2 style="DISPLAY: none" cellSpacing=0 cellPadding=0 			width=150 border=0>			<TR height=20>			<TD align=middle width=30><IMG height=9 			src="images/menu_icon.gif" width=9></TD>			<TD><A href="guest/main_right2-1.php" 			    	target="rightFrame">课程简介</A></TD></TR>			<TR height=20>			<TD align=middle width=30><IMG height=9 			src="images/menu_icon.gif" width=9></TD>			<TD><A href="guest/main_right2-2.php" 			    	target="rightFrame">教师团队</A></TD></TR>			<TR height=20>			<TD align=middle width=30><IMG height=9 			src="images/menu_icon.gif" width=9></TD>			<TD><A href="guest/main_right2-3.php" 			    	target="rightFrame">教学条件</A></TD></TR>			<TR height=4>			<TD colSpan=2></TD></TR>			</TABLE><!-- 第三选项 --><TABLE cellSpacing=0 cellPadding=0 width=150 border=0><TR height=30><TD style="PADDING-LEFT: 20px; FONT-SIZE: 15px" background=images/menu_bt.jpg>          	<A class=menuParent οnclick=expand(3)             	href="javascript:void(0);">互动交流</A></TD></TR><TR height=4><TD></TD></TR></TABLE><TABLE id=child3 style="DISPLAY: block" cellSpacing=0 cellPadding=0 width=150 border=0><TR height=20><TD align=middle width=30><IMG height=9 src="images/menu_icon.gif" width=9></TD><TD><A href="main_right3-1.php" 			    	target="rightFrame">BBS论坛</A></TD></TR><TR height=20><TD align=middle width=30><IMG height=9 src="images/menu_icon.gif" width=9></TD><TD><A href="main_right3-2.php" 			    	target="rightFrame">通知公告</A></TD></TR><TR height=4><TD colSpan=2></TD></TR></TABLE></BODY></HTML>

        The displayed effect is as follows: 

 

 

2. Realize BBS Forum and Reply

        The forum is mainly implemented through Httppost.php under the custom database folder, where main_right3-1.php is the subject of all BBS forum posts, and main_right3-1-content.php is the content of the corresponding post, including reply content, etc.
        The code of main_right3-1.php is as follows:

 

<?php header("Content-Type: text/html; charset=utf-8"); //Note: include a php file cannot be repeated, recursively check whether the include ("../database/httppost.php) is repeated ");?><!-- It should be noted that ".." in Html means back to the previous directory and css is at the same level as main_right3-1.php --><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 >Current location: BBS Forum< /TD><TD align=right><A href="main_right.php" class="one"> Back</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><div class= "middle"><div class="wrap">	<div class="wrap_left">    	<div class="wenda-head"><a href="#" class="quealltab  onactive" >最新</a><a href="#" class="quealltab " >热门</a><a href="#" class="quealltab " >等待回复</a>    	</div>    	<?php    		//查询贴				$hgi=new HttpPostInf();				$result=$hgi->doquery('2',"select * from BBS_Post;");				//解析json				$dj=json_decode($result,true);				$jcount=count($dj);				for($i=0; $i<$jcount; $i++){	    	?><div class="wenda-list">        	<div class="headslider">            	<img src='images/bbs3.jpg' class="wenda_list_head" width='40' height='40' />      				<a href="" title="name" class="wenda-nickname"><?php echo $dj[$i]['BP_Pid']; ?></a></div><div class="qaslider"><ul><li><img src="images/bbs1.jpg" class="icon"/><a                     		href="main_right3-1-content.php?bbsid=<?php echo $dj[$i]['BP_Pid']; ?>"                     		class="wendatitle"><?php echo $dj[$i]['BP_Ptitle']; ?></a></li><li><img src="images/bbs2.png" class="icon2"/><span class="replydet">发帖身份:                    	<?php                     		if($dj[$i]['BP_Ptype']==0) echo "教师";                    		if($dj[$i]['BP_Ptype']==1) echo "学生";                    	?>  |  发帖人编号:<?php echo $dj[$i]['BP_Puserid']; ?></span></li><li><span class="time">提问时间:<?php echo $dj[$i]['BP_Ptime']; ?></span></ul></div><!--qaslider--></div><!--wenda-list--><?php    	//结束循环    	}	    ?></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>TABLE cellSpacing=0 cellPadding=2 width="95%" align=center border=0 >	<TR height=20><TD></TD></TR></TABLE>TABLE cellSpacing=0 cellPadding=2 width="95%" align=center border=0 >	<TR height=20><TD></TD></TR></TABLE>

        Here you need to point out that the ".." in the HTML code "../database/httppost.php" means to return to the upper level directory. The code directory is shown in the figure below, and the source code is placed in the htdocs folder in Apache. 

 

        The main_right3-1-content.php code is as follows: 

<?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');	}?>

        The post reply display effect is shown in the figure below: 

 

        The code posted will not be posted, you can view the source code. At this point, you may be confused about the content of the database access part, let's briefly explain it below!
 

Three. PHP defines classes and member functions

        In PHP, the websites we usually complete during course study are based on a process-oriented approach, thus ignoring its object-oriented knowledge. I obviously made this mistake.
        PS: Since this project is a coursework of "Object-Oriented Analysis and Development", it needs to use object-oriented knowledge to analyze and realize. Suddenly one day I discovered that PHP used to implement HTML and PHP layout backends, and didn't use the knowledge of classes, encapsulation, and inheritance, but after consulting relevant information, I found that it all has these things.
        Later, in the process of implementing this project, we defined different classes (thanks to PU brother). Here are only examples of database and Human classes.

<?phpheader("Content-Type: text/html; charset=utf-8");class HttpPostInf{    function __construct(){ //无参构造函数    }    function doquery($type , $sql){ //网路通信函数			$data = array ('sqlType' => $type , 'sqlExe' => $sql);			$data = http_build_query($data);			$opts = array ('http' => array ('method' => 'POST','header'=>				"Content-type: application/x-www-form-urlencoded\r\n" ."Content-Length: "				. strlen($data) . "\r\n",'content' => $data));			$context = stream_context_create($opts);			$html = file_get_contents('http://courseweb.sinaapp.com/courseweb.php', false, $context);			return $html;	}	}?>

        Define the Human class at the same time, and the Student and Teacher inheriting this class are login users with two identities. The constructor and instantiation have been written in the member function. 

<?phpheader("Content-Type: text/html; charset=utf-8");include_once("httppost.php");//Human, has the common attributes and methods of teachers and students class Human{ private $hid; //Unique number private $pwd; //Password private $name; //Name private $sex; //Gender private $email; //Mailbox function __construct(){ //No parameter constructor} //Static verification function static function id_vf($id,$spwd,$type){ //Class static method type=1 is student, 2 is teacher $hpi=new HttpPostInf(); if($type==1) {$result=$hpi- >doquery('2',"select * from Student_Info where Sinf_id='".$id."';"); if($result =='error' || $result=='null'){ //Return No value return -1; //No user, return 0 }else{ $dj=json_decode($result,true);$jcount=count($dj);for($i=0; $i<$jcount; $ i++){ //There is only one password $right_pwd=$dj[$i]['SInf_Pwd']; //Student login password} if($spwd == $right_pwd) return 1;//Verification is successful, you can log in else return 0;//password error}} else if($type==2) {$result=$hpi->doquery('2',"select * from Teacher_Info where TI_id='" .$id."';"); if($result =='error' || $result=='null'){ //Returns no value return -1; //No user, return 0 }else{ $ dj=json_decode($result,true);$jcount=count($dj);for($i=0; $i<$jcount; $i++){ //Only one password $right_pwd=$dj[$i] ['TI_Pwd']; //Student login password} if($spwd==$right_pwd) return 1; //Verification is successful, you can log in else return 0;//password error}} }}class Student extends Human{ private $ bir; //Date of birth private $maj; //Professional private $eym; //Enrollment year private $score; //Score function __construct($sid){ //1 parameter constructor $this->hid=$sid; $stu=new HttpPostInf(); $result=$stu->doquery('2',"select * from Student_Info where Sinf_ID='".$sid."';");//echo 'result:'.$result;		//解析json		$dj=json_decode($result,true);		$jcount=count($dj);		for($i=0; $i<$jcount; $i++){			//只返回1个,所以只取一个即可,jcount=1			$this->bir=$dj[$i]['SInf_Bir'];			$this->maj=$dj[$i]['SInf_Maj'];			$this->eym=$dj[$i]['SInf_Eym'];			$this->score=$dj[$i]['SInf_Score'];//或者写:$score=$dj->Sinf_Bir;			$this->pwd=$dj[$i]['SInf_Pwd'];			$this->name=$dj[$i]['SInf_Name'];			$this->sex=$dj[$i]['SInf_Sex'];			$this->email=$dj[$i]['SInf_Email'];			}	}function getname(){return $this->name;}	function getsex(){return $this->sex;}	function getemail(){return $this->email;}	function getpwd(){return $this->pwd;}	function getbir(){return $this->bir;}	function getmaj(){return $this->maj;}	function geteym(){return $this->eym;}	function getscore(){return $this->score;}}class Teacher extends Human{	private $ptitle; //职称	private $res;    //成果	private $award;  //获奖		function __construct($tid){ //1参构造函数		$this->hid=$tid;		$stu=new HttpPostInf();			$result=$stu->doquery('2',"select * from Teacher_Info where TI_ID='".$tid."';");		//echo 'result:'.$result;		//解析json		$dj=json_decode($result,true);		$jcount=count($dj);		for($i=0; $i<$jcount; $i++){			//只返回1个,所以只取一个即可,jcount=1			$this->ptitle=$dj[$i]['TI_Ptitle'];			$this->res=$dj[$i]['TI_Res'];			$this->award=$dj[$i]['TI_Award'];//或者写:$score=$dj->Sinf_Bir;			$this->pwd=$dj[$i]['TI_Pwd'];			$this->name=$dj[$i]['TI_Name'];			$this->sex=$dj[$i]['TI_Sex'];			$this->email=$dj[$i]['TI_Email'];						}		}function getname(){return $this->name;}		function getsex(){return $this->sex;}		function getemail(){return $this->email;}		function getpwd(){return $this->pwd;}		function getptitle(){return $this->ptitle;}		function getres(){return $this->res;}		function getaward(){return $this->award;}}?>

        Then call the classes HttpPostInf and Human defined above, and the simple code to access the database is as follows:
        <?php
                //Query post
                $hgi=new HttpPostInf();
                $result=$hgi->doquery('2',"select * from BBS_Post;");//SQL statement
                //Parse json
                $dj=json_decode($result,true);
                $jcount=count($dj);
                for($i=0; $i<$jcount; $i++ ) {   
                        echo $dj[$i]['BP_Pid'];
                }
        ?>
        When the user logs in, the Session will be used to record the user’s id, and then all the user’s information can be obtained through class instantiation. The simple code is as follows :
        $h1=new Student('2220140598');
        echo $h1->getname(); echo'<br>';
        echo $h1->getsex();echo '<br>';
        echo $h1->getemail(); echo'<br>';

        Finally, I hope the article will be helpful to everyone, especially I found that there are few articles built in PHP! A relatively large number of articles deal with some details of PHP, and I hope you learn more about PHP. I always feel that I have some problems with blogging, but I can’t tell them. I hope everyone can put forward the shortcomings! The code database is laid out on the SAE, so the effect of all the above graphics can be displayed directly.
      (By: Eastmount 2015-3-14 at 4 pm   http://blog.csdn.net/eastmount/ )

Guess you like

Origin blog.csdn.net/zy17822307856/article/details/112798413