显示效果如下: 2. 实施 BBS论坛及回复 论坛主要是通过自定义数据库文件夹下的Httppost.php来实现的。 main_right3-1.php是显示所有BBS论坛帖子的主题,main_right3-1-content.php是对应帖子的内容。包括回复内容等 main_right3-1.php的代码如下:
最新热门等待回复doquery('2',"select * from BBS_Post;");//解析json$dj=json_decode($result,true);$jcount= count ($dj);for($i=0; $i<$jcount; $i++){ ?>
| ?夹紧。 right3-1-content.php的代码如下:
doquery('2',"从 BBS_Post 中选择*,其中 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'];
?>
doquery('1',$sql);//echo "";header('位置:main_right3-1.php'); }
?> 帖 发帖展示效果如下图: 帖子的代码不会被发布,你可以查看源代码。写完这里,你可能会对数据库访问部分的内容感到困惑。下面就简单的解释一下吧!
3. PHP 定义了类和成员函数 在 PHP 中,我们通常在课程学习中完成的网站都是基于面向过程的方法,从而忽略了其面向对象的知识,我显然也犯了这个错误。 PS:由于本项目是《面向对象分析与开发》课程作业,因此需要面向对象的知识来分析和实现。突然有一天我发现PHP以前都是在HTML和PHP布局后台实现的,并没有用到类、封装、继承的知识。不过,查了相关资料后,发现都是有这些东西的。 后来在这个项目的实施过程中,我们定义了不同的类(感谢PU哥)。这里仅是数据库和人类类的示例。
$type, 'sqlExe' => $sql) ; $data = http_build_query($data); $opts = array ('http' => array ('method' => 'POST','header'=> "内容类型: 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://www.sychzs.cn/courseweb.php', false, $context);return $html;}
}
?> 同时定义了Human类,继承该类的Student和Teacher是具有两种身份的登录用户。构造函数和实例化已经写在成员函数中了。
doquery('2',"从 Student_Info 中选择 *,其中 Sinf_id='".$id."';");if($result =='error ' || $result==' null'){ //无返回值 return -1; //没有用户,返回0}else{$dj=json_decode($result,true);$jcount=count($dj);for($i=0; $i<$jcount; $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; //无user, return 0}else{$dj=json_decode($result,true);$jcount=count($dj);for($i=0; $i< $jcount; $i++){//只有一个密码 $ right_pwd=$dj[$i]['TI_Pwd']; //学生登录密码} if($spwd==$right_pwd) return 1; //验证成功,可以登录 else return 0;//密码错误 } }}}class Student 扩展了 Human{private $bir; //属性类型私有$may; //私人$eym; //公共私有$score; //标准函数 __construct($sid){ //第一小时键盘 $this->hid=$sid;$stu=new HttpPostInf(); $result=$stu->doquery('2',"从 Student_Info 中选择 *,其中 Sinf_ID='".$sid."';"); =json_decode($result,true);$jcount=count($dj);for($i=0;$i<$jcount; $i++){//只返回1个,所以只取一个即可,jcount=1$this->bir=$dj[$i]['SInf_Bir'];$this->may= $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 getmay() {return $this->may;} function gettime(){return $this->eym;} function getscore(){return $this->score;}}class Teacher 扩展了 Human{ private $ptitle; //默认私有$res; //私人$奖励; //enabled function __construct($time){ //第一小时关键字 $this->hid=$time;$stu=new HttpPostInf(); $result=$stu->doquery('2',"从 Teacher_Info 中选择 *,其中 TI_ID='".$time."';"); = 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'];$dj[$i]['YOUR_Pwd'];$this->name=$dj [$i]['YOUR_Name'];$this->sex=$dj[$i]['YOUR_SEX'];$this ->email=$dj[$i]['YOUR_Email']; } }function getname (){return $this->name;}function getsex(){return $this->sex;}function getname(){return $this->email;} function getpwd() {return $this->pwd;} function getptitle(){return $this->ptitle;} function getres(){return $this->res;} function getaward() { return $this ->ward ;}
} }?> 然后就可以调用上面定义的类HttpPostInf和Human了,访问数据库的简单代码如下: //询价帖 $hgi=新选择*来自BBS_Post;");//SQL语句 $jcount=计数($dj); for($i=0; $i<$jcount; $i++) { } ?> 当用户登录时,会使用Session来记录用户的id,然后通过类实例化就可以获取到用户的所有信息。简单代码如下: $h1=new Student('2220140598'); echo $h1->getname(); echo ' '; ; echo $h1->getemail(); echo ' '; 最后希望文章对大家有帮助,特别是我发现关于PHP搭建的文章很少!相对来说很多文章都是关于PHP处理的某些细节,也希望大家能够学习PHP相关知识。我总觉得我写博客有一些问题,但我自己又无法解释。希望大家指出不足之处!代码数据库是在SAE上布局的,所以直接运行以上所有图形的效果都能出现就行了。 (作者:Eastmount 2015-3-14 4:00 pm http://www.sychzs.cn/eastmount/)
|
|