background="images/header_bg.gif" border=0>

您还未登录! |
登录|
学生注册|
src="main_right.php" name="rightFrame" id="rightFrame" title="rightFrame">
include('main_left.php');
?>
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;
}在Iframe中主要的代码是,调用时“BBS论坛”声明target为rightFrame即可。同时隐藏显示设置style.display为block或none。
其中main_left.php代码如下,此时即可实现上面的布局。
function expand(el) {
childObj = document.getElementById("child" + el);
if (childObj.style.display == 'none') {
childObj.style.display = 'block';
} else {
childObj.style.display = 'none';
}
return;
}
background=images/menu_bg.jpg border=0 align=left>
href="javascript:void(0);">课程首页 |
width=150 border=0>
src="images/menu_icon.gif" width=9>
href="javascript:void(0);">课程概况 |
width=150 border=0>
src="images/menu_icon.gif" width=9>
src="images/menu_icon.gif" width=9>
src="images/menu_icon.gif" width=9>
href="javascript:void(0);">互动交流 |
width=150 border=0>
src="images/menu_icon.gif" width=9>
src="images/menu_icon.gif" width=9>
显示的效果如下:

二. 实现BBS论坛及回复
论坛主要通过自定义类database文件夹下Httppost.php实现,其中main_right3-1.php是显示所有BBS论坛帖子的主题,而main_right3-1-content.php是对应帖子的内容,包括回复内容等。
其中main_right3-1.php的代码如下:
header("Content-Type: text/html; charset=utf-8");
//注意:include一个php文件不能重复,要递归检查是否重复引用
include ("../database/httppost.php");
?>
| 当前位置: BBS论坛 | 返回 |

其中main_right3-1-content.php代码如下:
header("Content-Type: text/html; charset=utf-8");
include ("../database/human.php");
session_start();
?>
| 当前位置: BBS论坛 | 返回 |
//查询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
$_SESSION['bbsid']=$dj[$i]['BP_Pid'];
?>
align=left>[贴] <?php echo $dj[$i]['BP_Ptitle']; ?> |
|
src='images/tx.jpg' width='60' height='60' />
if($dj[$i]['BP_Ptype']==0) echo "教师"; if($dj[$i]['BP_Ptype']==1) echo "学生"; ?><?php echo $dj[$i]['BP_Ptime']; ?> | maxlength="2000" name="content" cols="60" disabled="disabled"><?php echo $dj[$i]['BP_Cont']; ?> | |
|
| ||
}
//查询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
?>
|
if($dj[$i]['BR_Rtype']==0) echo "教师"; if($dj[$i]['BR_Rtype']==1) echo "学生"; ?>
| maxlength="2000" name="content" cols="60" disabled="disabled"><?php echo $dj[$i]['BR_Cont']; ?> | |
|
| ||
}
?>
| 回复楼层 | ||
|
| name="bbscontent" cols="60" > | |
|
| ||
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 "";
header('Location:main_right3-1.php');
}
?>发帖回复显示效果如下图所示:


发帖的代码就不贴出来了,可以查看源代码。写到此处可能你会疑惑数据库访问部分的内容,下面进行简单讲解吧!
三. PHP定义类及成员函数
在PHP中我们通常课程学习时完成的网站都是基于面向过程的方法,从而忽略了它的面向对象知识,我显然也犯了这样的错误。
PS:由于该项目是《面向对象分析与开发》课程作业,所以需要用面向对象的知识去分析和实现。突然有一天我发现PHP以前用的都是HTML和PHP布局后台实现的,没用用到类、封装、继承的知识,但查阅相关资料后发现它都是有这些东西的。
后来在实现这个项目过程中我们定义了不同的类(感谢PU哥),这里仅仅举例数据库方面和Human类。<?php
header("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;
}
}
?>同时定义Human类,而继承该类的Student和Teacher是两种身份的登陆用户。其中构造函数、实例化都已经在成员函数中写好。
header("Content-Type: text/html; charset=utf-8");
include_once("httppost.php");
//人类,拥有教师和学生的共同属性和方法
class Human{
private $hid; //唯一编号
private $pwd; //密码
private $name; //名字
private $sex; //性别
private $email; //邮箱
function __construct(){ //无参构造函数
}
//静态验证函数
static function id_vf($id,$spwd,$type){ //类静态方法 type=1为学生,2为老师
$hpi=new HttpPostInf();
if($type==1)
{
$result=$hpi->doquery('2',"select * from Student_Info where Sinf_id='".$id."';");
if($result =='error' || $result=='null'){ //返回无值
return -1; //没有用户,返回0
}
else{
$dj=json_decode($result,true);
$jcount=count($dj);
for($i=0; $i
//只有一个密码
$right_pwd=$dj[$i]['SInf_Pwd']; //学生登录密码
}
if($spwd == $right_pwd) return 1; //验证成功,可以登陆
else return 0;//密码错误
}
}
else if($type==2)
{
$result=$hpi->doquery('2',"select * from Teacher_Info where TI_id='".$id."';");
if($result =='error' || $result=='null'){ //返回无值
return -1; //没有用户,返回0
}
else{
$dj=json_decode($result,true);
$jcount=count($dj);
for($i=0; $i
//只有一个密码
$right_pwd=$dj[$i]['TI_Pwd']; //学生登录密码
}
if($spwd==$right_pwd) return 1; //验证成功,可以登陆
else return 0;//密码错误
}
}
}
}
class Student extends Human{
private $bir; //出生日期
private $maj; //专业
private $eym; //入学年份
private $score; //得分
function __construct($sid){ //1参构造函数
$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
//只返回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
//只返回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;}
}
?>再调用上面定义的类HttpPostInf和Human,实现访问数据库的简单代码如下所示:
$hgi=new HttpPostInf();
$result=$hgi->doquery(‘2‘,"select * from BBS_Post;");//SQL语句
//解析json
$dj=json_decode($result,true);
$jcount=count($dj);
for($i=0; $i
echo $dj[$i][‘BP_Pid‘];
}
?>
而当用户登录后会使用Session记录用户的id,然后通过类实例化即可实现获取该用户的所有信息,简单的代码如下:
$h1=new Student(‘2220140598‘);
echo $h1->getname(); echo ‘
‘;
echo $h1->getsex(); echo ‘
‘;
echo $h1->getemail(); echo ‘
‘;
最后希望文章对大家有所帮助,尤其发现PHP方面搭建的文章比较少!相对多的文章是PHP关于某个细节处理,同时更希望大家学习下PHP类相关的知识。总感觉自己写博客有些问题,但自己说不出来,希望大家提出不足之处!该代码数据库是布局在SAE上的,所以直接运行可以出现上面所有图形的效果。







