本文主要讲述如何通过php+html简单实现bbs论坛和发帖/回帖的功能,这是提取我们php项目的部分内容。主要内容包括:
1.通过javascript和iframe实现局部布局界面
2.php如何定义类实现访问数据库功能
3.实现简单的bbs论坛和发帖/回帖功能
由于这个项目是十个人在寒假完成,所以采用了sae搭建在线的后台数据库,其他人在通过apache本地设计网页、访问数据库的方法实现,相当于简单的bs三端访问。
源码下载地址:
访问 http://localhost:8080/testphp/index.php效果如下图所示:
一. 首页布局搭建 首先是搭建首页的布局,主要布局分为head、main_left和main_right。其中右边显示部分采用的是iframe实现的局部显示,而左边采用javascript实现点击隐藏和显示功能。其中index.php首页代码如下:
《分布式系统》精品课程学习 您还未登录! | 登录| 学生注册| 教师注册
这里的css代码布局调用css文件中的mycss.css实现:
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;}
在iframe中主要的代码是,调用时“bbs论坛”声明target为rightframe即可。同时隐藏显示设置style.display为block或none。
其中main_left.php代码如下,此时即可实现上面的布局。
课程首页
首页介绍
课程概况
课程简介
教师团队
教学条件
互动交流
bbs论坛
通知公告
显示的效果如下:
二. 实现bbs论坛及回复 论坛主要通过自定义类database文件夹下httppost.php实现,其中main_right3-1.php是显示所有bbs论坛帖子的主题,而main_right3-1-content.php是对应帖子的内容,包括回复内容等。
其中main_right3-1.php的代码如下:
当前位置: bbs论坛 返回
最新 热门 等待回复
doquery('2',select * from bbs_post;); //解析json $dj=json_decode($result,true); $jcount=count($dj); for($i=0; $i
发帖身份: | 发帖人编号: 提问时间:
这里需要指出html代码../database/httppost.php中..表示返回上一级目录,该代码的目录如下图所示,源代码放在apache中htdocs文件夹中。
其中main_right3-1-content.php代码如下:
当前位置: bbs论坛 返回
doquery('2',select * from bbs_post where bp_pid='.$_get['bbsid'].';); //解析json $dj=json_decode($result,true); $jcount=count($dj); for($i=0; $i
[贴]
楼主 1#
身份
doquery('2',select * from bbs_reply where br_pid='.$_get['bbsid'].';); //解析json $dj=json_decode($result,true); $jcount=count($dj); for($i=0; $i
楼层 #
身份
回复楼层#
回复楼层
回复
doquery('1',$sql); //echo ; header('location:main_right3-1.php'); }?> 发帖回复显示效果如下图所示:
发帖的代码就不贴出来了,可以查看源代码。写到此处可能你会疑惑数据库访问部分的内容,下面进行简单讲解吧!
三. php定义类及成员函数 在php中我们通常课程学习时完成的网站都是基于面向过程的方法,从而忽略了它的面向对象知识,我显然也犯了这样的错误。
ps:由于该项目是《面向对象分析与开发》课程作业,所以需要用面向对象的知识去分析和实现。突然有一天我发现php以前用的都是html和php布局后台实现的,没用用到类、封装、继承的知识,但查阅相关资料后发现它都是有这些东西的。
后来在实现这个项目过程中我们定义了不同的类(感谢pu哥),这里仅仅举例数据库方面和human类。
$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是两种身份的登陆用户。其中构造函数、实例化都已经在成员函数中写好。
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; $idoquery('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; $ihid=$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; $ibir=$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; $iptitle=$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,实现访问数据库的简单代码如下所示:
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上的,所以直接运行可以出现上面所有图形的效果。
(by:eastmount 2015-3-14 下午4点 )