当前位置:   article > 正文

PHP+HTML简单实现BBS论坛与回帖_html论坛代码

html论坛代码
        本文主要讲述如何通过PHP+HTML简单实现BBS论坛和发帖/回帖的功能,这是提取我们php项目的部分内容。主要内容包括:
        1.通过JavaScript和Iframe实现局部布局界面
        2.PHP如何定义类实现访问数据库功能
        3.实现简单的BBS论坛和发帖/回帖功能
        由于这个项目是十个人在寒假完成,所以采用了SAE搭建在线的后台数据库,其他人在通过Apache本地设计网页、访问数据库的方法实现,相当于简单的BS三端访问。
        源码下载地址:http://download.csdn.net/detail/eastmount/8501443
        访问http://localhost:8080/testphp/index.php效果如下图所示:



一. 首页布局搭建

        首先是搭建首页的布局,主要布局分为head、main_left和main_right。其中右边显示部分采用的是iframe实现的局部显示,而左边采用JavaScript实现点击隐藏和显示功能。其中index.php首页代码如下:
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>《分布式系统》精品课程学习</title>
  5. <link href="css/mycss.css" type="text/css" rel="stylesheet"/>
  6. <style type="text/css">
  7. /* 这个链接改变颜色 */
  8. a.one:link {color: #ff0000}
  9. a.one:visited {color: #0000ff}
  10. a.one:hover {color: #ffcc00}
  11. </style>
  12. </head>
  13. <body>
  14. <!-- 布局顶部登录状态 -->
  15. <div id="main">
  16. <TABLE cellSpacing=0 cellPadding=0 width="100%"
  17. background="images/header_bg.gif" border=0>
  18. <!--头部图片-->
  19. <TR height=80>
  20. <TD width=260>
  21. <IMG height=80 src="images/logo.png" width=260>
  22. </TD>
  23. <TD style="FONT-SIZE: 12px; FONT-WEIGHT: bold; COLOR: #000;
  24. PADDING-TOP: 20px; PADDING-RIGHT: 20px" align=right>
  25. 您还未登录! |
  26. <A class="one" href="login.php" >登录</A>|
  27. <A class="one" href="register_student.php" >学生注册</A>|
  28. <A class="one" href="register_teacher.php" >教师注册</A>
  29. </TD>
  30. </TR>
  31. </TABLE>
  32. <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  33. <TR bgColor=#1c5db6 height=4>
  34. <TD></TD></TR>
  35. </TABLE>
  36. <br />
  37. <!-- 布局中部 -->
  38. <div id="middle">
  39. <!-- 布局中部右边 -->
  40. <div id="index_right">
  41. <iframe height="100%" width="100%" border="0" frameborder="0"
  42. src="main_right.php" name="rightFrame" id="rightFrame" title="rightFrame">
  43. </iframe>
  44. </div>
  45. <!-- 布局中部左边 -->
  46. <div id="index_left">
  47. <?php
  48. include('main_left.php');
  49. ?>
  50. </div>
  51. </div>
        这里的CSS代码布局调用css文件中的mycss.css实现:
  1. html, body {
  2. height: '100%';
  3. width: '100%';
  4. }
  5. /*头部布局*/
  6. #main{
  7. margin-left:200px;
  8. width:950px;
  9. }
  10. /*中部布局*/
  11. #middle{
  12. width:950px;
  13. height:500px;
  14. background-color:#b6effa;
  15. }
  16. /*中部左边*/
  17. #index_left{
  18. float:left;
  19. margin-top:10px;
  20. margin-left:10px;
  21. width:180px;
  22. height:480px;
  23. }
  24. /*中部右边*/
  25. #index_right{
  26. float:right;
  27. margin-top:10px;
  28. margin-right:20px;
  29. width:720px;
  30. height:480px;
  31. border-style:double;
  32. }
  33. /*中部右边*/
  34. #logout_middle{
  35. float:right;
  36. margin-top:100px;
  37. margin-right:220px;
  38. width:500px;
  39. height:300px;
  40. border-style:double;
  41. background-color:#ffffff;
  42. }
        在Iframe中主要的代码是<iframe src="main_right.php" name="rightFrame" id="rightFrame">,调用时“<A href="main_right3-1.php" target="rightFrame">BBS论坛</A>”声明target为rightFrame即可。同时隐藏显示设置style.display为block或none。
        其中main_left.php代码如下,此时即可实现上面的布局。

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML><HEAD>
  3. <META http-equiv=Content-Type content="text/html; charset=utf-8">
  4. <LINK href="css/admin.css" type="text/css" rel="stylesheet">
  5. <SCRIPT language=javascript>
  6. function expand(el) {
  7. childObj = document.getElementById("child" + el);
  8. if (childObj.style.display == 'none') {
  9. childObj.style.display = 'block';
  10. } else {
  11. childObj.style.display = 'none';
  12. }
  13. return;
  14. }
  15. </SCRIPT>
  16. </HEAD>
  17. <BODY>
  18. <TABLE height="100%" cellSpacing=0 cellPadding=0 width=170
  19. background=images/menu_bg.jpg border=0 align=left>
  20. <TR>
  21. <TD vAlign=top align=middle>
  22. <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  23. <TR>
  24. <TD height=10></TD></TR>
  25. </TABLE>
  26. <!-- 第一选项 -->
  27. <TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
  28. <TR height=30>
  29. <TD style="PADDING-LEFT: 20px; FONT-SIZE: 15px" background=images/menu_bt.jpg>
  30. <A class=menuParent οnclick=expand(1)
  31. href="javascript:void(0);">课程首页</A></TD></TR>
  32. <TR height=4>
  33. <TD></TD></TR>
  34. </TABLE>
  35. <TABLE id=child1 style="DISPLAY: none" cellSpacing=0 cellPadding=0
  36. width=150 border=0>
  37. <TR height=20>
  38. <TD align=middle width=30><IMG height=9
  39. src="images/menu_icon.gif" width=9></TD>
  40. <TD><A href="main_right1-1.php"
  41. target="rightFrame">首页介绍</A></TD></TR>
  42. <TR height=4>
  43. <TD colSpan=2></TD></TR>
  44. </TABLE>
  45. <!-- 第二选项 -->
  46. <TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
  47. <TR height=30>
  48. <TD style="PADDING-LEFT: 20px; FONT-SIZE: 15px" background=images/menu_bt.jpg>
  49. <A class=menuParent οnclick=expand(2)
  50. href="javascript:void(0);">课程概况</A></TD></TR>
  51. <TR height=4>
  52. <TD></TD></TR>
  53. </TABLE>
  54. <TABLE id=child2 style="DISPLAY: none" cellSpacing=0 cellPadding=0
  55. width=150 border=0>
  56. <TR height=20>
  57. <TD align=middle width=30><IMG height=9
  58. src="images/menu_icon.gif" width=9></TD>
  59. <TD><A href="guest/main_right2-1.php"
  60. target="rightFrame">课程简介</A></TD></TR>
  61. <TR height=20>
  62. <TD align=middle width=30><IMG height=9
  63. src="images/menu_icon.gif" width=9></TD>
  64. <TD><A href="guest/main_right2-2.php"
  65. target="rightFrame">教师团队</A></TD></TR>
  66. <TR height=20>
  67. <TD align=middle width=30><IMG height=9
  68. src="images/menu_icon.gif" width=9></TD>
  69. <TD><A href="guest/main_right2-3.php"
  70. target="rightFrame">教学条件</A></TD></TR>
  71. <TR height=4>
  72. <TD colSpan=2></TD></TR>
  73. </TABLE>
  74. <!-- 第三选项 -->
  75. <TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
  76. <TR height=30>
  77. <TD style="PADDING-LEFT: 20px; FONT-SIZE: 15px" background=images/menu_bt.jpg>
  78. <A class=menuParent οnclick=expand(3)
  79. href="javascript:void(0);">互动交流</A></TD></TR>
  80. <TR height=4>
  81. <TD></TD></TR>
  82. </TABLE>
  83. <TABLE id=child3 style="DISPLAY: block" cellSpacing=0 cellPadding=0
  84. width=150 border=0>
  85. <TR height=20>
  86. <TD align=middle width=30><IMG height=9
  87. src="images/menu_icon.gif" width=9></TD>
  88. <TD><A href="main_right3-1.php"
  89. target="rightFrame">BBS论坛</A></TD></TR>
  90. <TR height=20>
  91. <TD align=middle width=30><IMG height=9
  92. src="images/menu_icon.gif" width=9></TD>
  93. <TD><A href="main_right3-2.php"
  94. target="rightFrame">通知公告</A></TD></TR>
  95. <TR height=4>
  96. <TD colSpan=2></TD></TR>
  97. </TABLE>
  98. </BODY>
  99. </HTML>
        显示的效果如下:

二. 实现BBS论坛及回复

        论坛主要通过自定义类database文件夹下Httppost.php实现,其中main_right3-1.php是显示所有BBS论坛帖子的主题,而main_right3-1-content.php是对应帖子的内容,包括回复内容等。
        其中main_right3-1.php的代码如下:
  1. <?php
  2. header("Content-Type: text/html; charset=utf-8");
  3. //注意:include一个php文件不能重复,要递归检查是否重复引用
  4. include ("../database/httppost.php");
  5. ?>
  6. <!-- 需要注意在Html中“..”表示回到上一级目录 而css与main_right3-1.php同级目录 -->
  7. <link rel="stylesheet" href="css/main.css" type="text/css" />
  8. <TABLE cellSpacing=0 cellPadding=0 width="100%" align=center border=0>
  9. <TR height=28>
  10. <TD >当前位置: BBS论坛</TD>
  11. <TD align=right><A href="main_right.php" class="one"> 返回 </A></TD>
  12. </TR>
  13. <TR>
  14. <TD bgColor=#b1ceef height=1 colspan="2"></TD></TR>
  15. <TR height=10>
  16. <TD background=images/shadow_bg.jpg colspan="2"></TD></TR>
  17. </TABLE>
  18. <div class="middle">
  19. <div class="wrap">
  20. <div class="wrap_left">
  21. <div class="wenda-head">
  22. <a href="#" class="quealltab onactive" >最新</a>
  23. <a href="#" class="quealltab " >热门</a>
  24. <a href="#" class="quealltab " >等待回复</a>
  25. </div>
  26. <?php
  27. //查询贴
  28. $hgi=new HttpPostInf();
  29. $result=$hgi->doquery('2',"select * from BBS_Post;");
  30. //解析json
  31. $dj=json_decode($result,true);
  32. $jcount=count($dj);
  33. for($i=0; $i<$jcount; $i++){
  34. ?>
  35. <div class="wenda-list">
  36. <div class="headslider">
  37. <img src='images/bbs3.jpg' class="wenda_list_head" width='40' height='40' />
  38. <a href="" title="name" class="wenda-nickname"><?php echo $dj[$i]['BP_Pid']; ?></a>
  39. </div>
  40. <div class="qaslider">
  41. <ul>
  42. <li><img src="images/bbs1.jpg" class="icon"/><a
  43. href="main_right3-1-content.php?bbsid=<?php echo $dj[$i]['BP_Pid']; ?>"
  44. class="wendatitle"><?php echo $dj[$i]['BP_Ptitle']; ?></a></li>
  45. <li><img src="images/bbs2.png" class="icon2"/><span class="replydet">发帖身份:
  46. <?php
  47. if($dj[$i]['BP_Ptype']==0) echo "教师";
  48. if($dj[$i]['BP_Ptype']==1) echo "学生";
  49. ?>
  50.   |  发帖人编号:
  51. <?php echo $dj[$i]['BP_Puserid']; ?></span></li>
  52. <li><span class="time">提问时间:<?php echo $dj[$i]['BP_Ptime']; ?></span>
  53. </ul>
  54. </div><!--qaslider-->
  55. </div><!--wenda-list-->
  56. <?php
  57. //结束循环
  58. }
  59. ?>
  60. </div><!--wrap-->
  61. </div><!--wrap_right-->
  62. </div><!--middle-->
  63. <TABLE cellSpacing=0 cellPadding=2 width="95%" align=center border=0 >
  64. <TR height=20>
  65. <TD></TD>
  66. </TR>
  67. </TABLE>
        这里需要指出HTML代码"../database/httppost.php"中".."表示返回上一级目录,该代码的目录如下图所示,源代码放在Apache中htdocs文件夹中。

        其中main_right3-1-content.php代码如下:
  1. <?php
  2. header("Content-Type: text/html; charset=utf-8");
  3. include ("../database/human.php");
  4. session_start();
  5. ?>
  6. <link rel="stylesheet" href="css/main.css" type="text/css" />
  7. <TABLE cellSpacing=0 cellPadding=0 width="100%" align=center border=0>
  8. <TR height=28>
  9. <TD background=images/title_bg1.jpg>当前位置: BBS论坛</TD>
  10. <TD align=right><A href="main_right6-1.php" class="one"> 返回 </A></TD>
  11. </TR>
  12. <TR>
  13. <TD bgColor=#b1ceef height=1 colspan="2"></TD></TR>
  14. <TR height=10>
  15. <TD background=images/shadow_bg.jpg colspan="2"></TD></TR>
  16. </TABLE>
  17. <!-- PHP数据库中获取内容 -->
  18. <?php
  19. //查询BBS
  20. $hgi=new HttpPostInf();
  21. $result=$hgi->doquery('2',"select * from BBS_Post where BP_Pid='".$_GET['bbsid']."';");
  22. //解析json
  23. $dj=json_decode($result,true);
  24. $jcount=count($dj);
  25. for($i=0; $i<$jcount; $i++){
  26. $_SESSION['bbsid']=$dj[$i]['BP_Pid'];
  27. ?>
  28. <div class="middle">
  29. <div class="wrap">
  30. <div class="wrap_left">
  31. <div class="wenda-head">
  32. <TABLE cellSpacing=0 cellPadding=0 width="100%" align=center border=0 bgcolor="blue">
  33. <TR height=10>
  34. <TD></TD></TR>
  35. <TR height=22>
  36. <TD style="PADDING-LEFT: 10px; FONT-WEIGHT: bold; COLOR: #ffffff"
  37. align=left>[贴] <?php echo $dj[$i]['BP_Ptitle']; ?></TD></TR>
  38. <TR height=10>
  39. <TD></TD></TR>
  40. </TABLE>
  41. </div>
  42. <!-- 楼主内容 -->
  43. <TABLE cellSpacing=0 cellPadding=0 width="95%" align=center border=0>
  44. <tr>
  45. <td valign="top"><div align="middle" width=150><BR /><BR /><img
  46. src='images/tx.jpg' width='60' height='60' />
  47. <BR /><BR />楼主 1# <BR />
  48. <?php echo $dj[$i]['BP_Puserid']; ?>
  49. <BR />身份
  50. <?php
  51. if($dj[$i]['BP_Ptype']==0) echo "教师";
  52. if($dj[$i]['BP_Ptype']==1) echo "学生";
  53. ?><BR /><?php echo $dj[$i]['BP_Ptime']; ?></div></td>
  54. <!-- 多行文本输入控件 disabled="disabled"不可编辑 -->
  55. <td ><textarea rows="15" type="text" width="400px" style="resize:none;font-size:16px;"
  56. maxlength="2000" name="content" cols="60" disabled="disabled"><?php echo $dj[$i]['BP_Cont']; ?></textarea></td>
  57. </tr>
  58. <TR height=4>
  59. <TD colspan="3"></TD>
  60. </TR>
  61. <tr>
  62. <td colspan="2"><hr width="100%" size="2" color="#FF0000"></td>
  63. </tr>
  64. </TABLE>
  65. <!--上面的php中while循环并没有结束,还有右括号-->
  66. <?php
  67. }
  68. //查询BBS
  69. $hgi=new HttpPostInf();
  70. $result=$hgi->doquery('2',"select * from BBS_Reply where BR_Pid='".$_GET['bbsid']."';");
  71. //解析json
  72. $dj=json_decode($result,true);
  73. $jcount=count($dj);
  74. for($i=0; $i<$jcount; $i++){
  75. ?>
  76. <!-- 楼层回复内容 -->
  77. <TABLE cellSpacing=0 cellPadding=0 width="95%" align=center border=0>
  78. <tr>
  79. <td valign="top"><div align="middle" width=150><BR /><BR />
  80. <img src='images/tx.jpg' width='60' height='60' />
  81. <BR /><BR />楼层 <?php $_SESSION['bbshfid']=time(); echo $i+2;?># <BR />
  82. <?php echo $dj[$i]['BR_Ruserid']; ?>
  83. <BR />身份
  84. <?php
  85. if($dj[$i]['BR_Rtype']==0) echo "教师";
  86. if($dj[$i]['BR_Rtype']==1) echo "学生";
  87. ?>
  88. <BR /><?php echo $dj[$i]['BR_Ptime']; ?>
  89. <BR />回复楼层<?php echo $dj[$i]['BR_Pfloor']; ?>#</div></td>
  90. <!-- 多行文本输入控件 disabled="disabled"不可编辑 -->
  91. <td ><textarea rows="15" type="text" width="400px" style="resize:none;font-size:16px;"
  92. maxlength="2000" name="content" cols="60" disabled="disabled"><?php echo $dj[$i]['BR_Cont']; ?></textarea></td>
  93. </tr>
  94. <TR height=4>
  95. <TD colspan="3">
  96. </TD>
  97. </TR>
  98. <tr>
  99. <td colspan="2"><hr width="100%" size="2" color="#FF0000"></td>
  100. </tr>
  101. </TABLE>
  102. <?php
  103. }
  104. ?>
  105. <!-- 回复内容 -->
  106. <form id="form1" name="form1" method="post" action="main_right3-1-content.php">
  107. <TABLE cellSpacing=0 cellPadding=0 width="95%" align=center border=0>
  108. <tr>
  109. <td colspan="2">  回复楼层
  110. <input type="text" name="lc" id="lc" value="" style=width:150pt; maxlength="50"/>
  111. </td>
  112. </tr>
  113. <tr>
  114. <td valign="top"><div align="middle" width=150>
  115. <BR /><BR />   回复   <BR /></div></td>
  116. <!-- 多行文本输入控件 disabled="disabled"不可编辑 -->
  117. <td ><textarea rows="10" type="text" width="400px" style="resize:none;font-size:16px;" maxlength="2000"
  118. name="bbscontent" cols="60" ></textarea></td>
  119. </tr>
  120. <tr>
  121. <td colspan="2" align="middle">
  122. <input type="submit" style='font-size:22px' name="Submit" value="提交回复"/>
  123. </td>
  124. </tr>
  125. <TR height=4><TD colspan="3"></TD></TR>
  126. <tr>
  127. <td colspan="2"><hr width="100%" size="2" color="#FF0000"></td>
  128. </tr>
  129. </TABLE>
  130. </form>
  131. </div><!--wrap-->
  132. </div><!--wrap_right-->
  133. </div><!--middle-->
  134. <TABLE cellSpacing=0 cellPadding=2 width="95%" align=center border=0>
  135. <TR height=20>
  136. <TD></TD></TR>
  137. </TABLE>
  138. <!-- 提交当前页面POST数据库表单处理 -->
  139. <?php
  140. if (!empty($_POST['bbscontent']))
  141. {
  142. if($_SESSION['radio']==1) $a=1; //学生
  143. if($_SESSION['radio']==2) $a=0; //老师
  144. //SQL更新
  145. $sql = "INSERT INTO BBS_Reply (BR_Rid,BR_Pid,BR_Ruserid,BR_Rtype,BR_Ptime,BR_Pfloor,BR_Cont) VALUES (";
  146. $sql .= "'".$_SESSION['bbshfid']."',";
  147. $sql .= "'".$_SESSION['bbsid']."',";
  148. $sql .= "'".$_SESSION['number']."',";
  149. $sql .= "'".$a."',";
  150. $sql .= "'".date('Y-m-d')."',";
  151. $sql .= "'".$_POST['lc']."',";
  152. $sql .= "'".$_POST['bbscontent']."'";
  153. $sql .= ");";
  154. //更新信息
  155. //echo $sql;
  156. $hgi=new HttpPostInf();
  157. $result=$hgi->doquery('1',$sql);
  158. //echo "<script>alert('恭喜你修改成功!');</script>";
  159. header('Location:main_right3-1.php');
  160. }
  161. ?>
        发帖回复显示效果如下图所示:

        发帖的代码就不贴出来了,可以查看源代码。写到此处可能你会疑惑数据库访问部分的内容,下面进行简单讲解吧!

三. PHP定义类及成员函数

        在PHP中我们通常课程学习时完成的网站都是基于面向过程的方法,从而忽略了它的面向对象知识,我显然也犯了这样的错误。
        PS:由于该项目是《面向对象分析与开发》课程作业,所以需要用面向对象的知识去分析和实现。突然有一天我发现PHP以前用的都是HTML和PHP布局后台实现的,没用用到类、封装、继承的知识,但查阅相关资料后发现它都是有这些东西的。
        后来在实现这个项目过程中我们定义了不同的类(感谢PU哥),这里仅仅举例数据库方面和Human类。
  1. <?php
  2. header("Content-Type: text/html; charset=utf-8");
  3. class HttpPostInf{
  4. function __construct(){ //无参构造函数
  5. }
  6. function doquery($type , $sql){ //网路通信函数
  7. $data = array ('sqlType' => $type , 'sqlExe' => $sql);
  8. $data = http_build_query($data);
  9. $opts = array ('http' => array ('method' => 'POST','header'=>
  10. "Content-type: application/x-www-form-urlencoded\r\n" ."Content-Length: "
  11. . strlen($data) . "\r\n",'content' => $data));
  12. $context = stream_context_create($opts);
  13. $html = file_get_contents('http://courseweb.sinaapp.com/courseweb.php', false, $context);
  14. return $html;
  15. }
  16. }
  17. ?>
        同时定义Human类,而继承该类的Student和Teacher是两种身份的登陆用户。其中构造函数、实例化都已经在成员函数中写好。
  1. <?php
  2. header("Content-Type: text/html; charset=utf-8");
  3. include_once("httppost.php");
  4. //人类,拥有教师和学生的共同属性和方法
  5. class Human{
  6. private $hid; //唯一编号
  7. private $pwd; //密码
  8. private $name; //名字
  9. private $sex; //性别
  10. private $email; //邮箱
  11. function __construct(){ //无参构造函数
  12. }
  13. //静态验证函数
  14. static function id_vf($id,$spwd,$type){ //类静态方法 type=1为学生,2为老师
  15. $hpi=new HttpPostInf();
  16. if($type==1)
  17. {
  18. $result=$hpi->doquery('2',"select * from Student_Info where Sinf_id='".$id."';");
  19. if($result =='error' || $result=='null'){ //返回无值
  20. return -1; //没有用户,返回0
  21. }
  22. else{
  23. $dj=json_decode($result,true);
  24. $jcount=count($dj);
  25. for($i=0; $i<$jcount; $i++){
  26. //只有一个密码
  27. $right_pwd=$dj[$i]['SInf_Pwd']; //学生登录密码
  28. }
  29. if($spwd == $right_pwd) return 1; //验证成功,可以登陆
  30. else return 0;//密码错误
  31. }
  32. }
  33. else if($type==2)
  34. {
  35. $result=$hpi->doquery('2',"select * from Teacher_Info where TI_id='".$id."';");
  36. if($result =='error' || $result=='null'){ //返回无值
  37. return -1; //没有用户,返回0
  38. }
  39. else{
  40. $dj=json_decode($result,true);
  41. $jcount=count($dj);
  42. for($i=0; $i<$jcount; $i++){
  43. //只有一个密码
  44. $right_pwd=$dj[$i]['TI_Pwd']; //学生登录密码
  45. }
  46. if($spwd==$right_pwd) return 1; //验证成功,可以登陆
  47. else return 0;//密码错误
  48. }
  49. }
  50. }
  51. }
  52. class Student extends Human{
  53. private $bir; //出生日期
  54. private $maj; //专业
  55. private $eym; //入学年份
  56. private $score; //得分
  57. function __construct($sid){ //1参构造函数
  58. $this->hid=$sid;
  59. $stu=new HttpPostInf();
  60. $result=$stu->doquery('2',"select * from Student_Info where Sinf_ID='".$sid."';");
  61. //echo 'result:'.$result;
  62. //解析json
  63. $dj=json_decode($result,true);
  64. $jcount=count($dj);
  65. for($i=0; $i<$jcount; $i++){
  66. //只返回1个,所以只取一个即可,jcount=1
  67. $this->bir=$dj[$i]['SInf_Bir'];
  68. $this->maj=$dj[$i]['SInf_Maj'];
  69. $this->eym=$dj[$i]['SInf_Eym'];
  70. $this->score=$dj[$i]['SInf_Score'];
  71. //或者写:$score=$dj->Sinf_Bir;
  72. $this->pwd=$dj[$i]['SInf_Pwd'];
  73. $this->name=$dj[$i]['SInf_Name'];
  74. $this->sex=$dj[$i]['SInf_Sex'];
  75. $this->email=$dj[$i]['SInf_Email'];
  76. }
  77. }
  78. function getname(){return $this->name;}
  79. function getsex(){return $this->sex;}
  80. function getemail(){return $this->email;}
  81. function getpwd(){return $this->pwd;}
  82. function getbir(){return $this->bir;}
  83. function getmaj(){return $this->maj;}
  84. function geteym(){return $this->eym;}
  85. function getscore(){return $this->score;}
  86. }
  87. class Teacher extends Human{
  88. private $ptitle; //职称
  89. private $res; //成果
  90. private $award; //获奖
  91. function __construct($tid){ //1参构造函数
  92. $this->hid=$tid;
  93. $stu=new HttpPostInf();
  94. $result=$stu->doquery('2',"select * from Teacher_Info where TI_ID='".$tid."';");
  95. //echo 'result:'.$result;
  96. //解析json
  97. $dj=json_decode($result,true);
  98. $jcount=count($dj);
  99. for($i=0; $i<$jcount; $i++){
  100. //只返回1个,所以只取一个即可,jcount=1
  101. $this->ptitle=$dj[$i]['TI_Ptitle'];
  102. $this->res=$dj[$i]['TI_Res'];
  103. $this->award=$dj[$i]['TI_Award'];
  104. //或者写:$score=$dj->Sinf_Bir;
  105. $this->pwd=$dj[$i]['TI_Pwd'];
  106. $this->name=$dj[$i]['TI_Name'];
  107. $this->sex=$dj[$i]['TI_Sex'];
  108. $this->email=$dj[$i]['TI_Email'];
  109. }
  110. }
  111. function getname(){return $this->name;}
  112. function getsex(){return $this->sex;}
  113. function getemail(){return $this->email;}
  114. function getpwd(){return $this->pwd;}
  115. function getptitle(){return $this->ptitle;}
  116. function getres(){return $this->res;}
  117. function getaward(){return $this->award;}
  118. }
  119. ?>
        再调用上面定义的类HttpPostInf和Human,实现访问数据库的简单代码如下所示:
        <?php
                
//查询贴
                $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<$jcount; $i++) {   
                        echo $dj[$i]['BP_Pid'];
                }
        ?>
        而当用户登录后会使用Session记录用户的id,然后通过类实例化即可实现获取该用户的所有信息,简单的代码如下:
        $h1=new Student('2220140598');
        echo $h1->getname(); echo '<br>';
        echo $h1->getsex(); echo '<br>';
        echo $h1->getemail(); echo '<br>';

        最后希望文章对大家有所帮助,尤其发现PHP方面搭建的文章比较少!相对多的文章是PHP关于某个细节处理,同时更希望大家学习下PHP类相关的知识。总感觉自己写博客有些问题,但自己说不出来,希望大家提出不足之处!该代码数据库是布局在SAE上的,所以直接运行可以出现上面所有图形的效果。
      (By:Eastmount 2015-3-14 下午4点  http://blog.csdn.net/eastmount/
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号