欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP实现的简单留言板功能示例【基于thinkPHP框架】

程序员文章站 2022-10-04 16:52:26
本文实例讲述了php实现的简单留言板功能。分享给大家供大家参考,具体如下: 入口文件  文件名 index.php

本文实例讲述了php实现的简单留言板功能。分享给大家供大家参考,具体如下:

入口文件  文件名 index.php

<?php
// 应用入口文件
// 检测php环境
if(version_compare(php_version,'5.3.0','<')) die('require php > 5.3.0 !');
// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false
define('app_debug',true);//开发调试模式
//define('app_debug',false);//生产模式
// 定义应用目录
define('app_path','./message/');
// 引入thinkphp入口文件
require './thinkphp/thinkphp.php';
// 亲^_^ 后面不需要任何代码了 就是如此简单

配置文件 文件名 config.php

<?php
return array(
  //'配置项'=>'配置值'
  'show_page_trace'=>true,
  'db_type'        => 'mysqli',   // 数据库类型
  'db_host'        => '127.0.0.1', // 服务器地址
  'db_name'        => 'msg',     // 数据库名
  'db_user'        => 'root',   // 用户名
  'db_pwd'        => 'root',     // 密码
  'db_port'        => '3306',    // 端口
  'db_prefix'       => 'ms_',  // 数据库表前缀
);

控制器  文件名 msgcontroller.class.php

<?php
namespace home\controller;
use think\controller;
use think\model;
class msgcontroller extends controller{
  public function index(){
    $msg = d('msg');
    $info = $msg->order('id desc')->select();
    $this->assign('info',$info);
    $this->display();
  }
  public function sendmsg(){
    $msg = new \home\model\msgmodel();
    if (!empty($_post)){
      $data = $msg->create();
      if($data){
        $data['user_hobby'] = implode(',',$data['user_hobby']);
        $z = $msg->add($data);
        if ($z){
          $this->redirect('msg/sendmsg');
        }
      }else{
        $this->assign('errorinfo',$msg->geterror());
      }
    }
    $this->display();
  }
  public function upd($id){
    $msg = d('msg');
    if (!empty($_post)){
      $z = $msg->save($_post);
      if ($z){
        $this->redirect('index',array(),2,'修改成功');
      }else{
        $this->redirect('upd',array('id'=>$id),2,'修改失败');
      }
    }else{
      $info = $msg->find($id);
      $this->assign('info',$info);
      $this->display();
    }
  }
  public function addmsg(){
    $msg = d('msg');
    if (!empty($_post)){
      $z = $msg->add($_post);
      if ($z){
        $this->redirect('index',array(),2,'添加成功');
      }else{
        $this->redirect('addmsg',array(),2,'添加失败');
      }
    }else{
      $this->display();
    }
  }
  public function del($id){
    if(d('msg')->delete($id)){
      $this->success('成功',u('index'),2);
    }else{
      $this->error('失败',u('index'),2);
    }
  }
}

模板  文件名 msgmodel.class.php

<?php
namespace home\model;
use think\model;
class msgmodel extends model{
  //是否批量验证
  protected $patchvalidate = true;
  protected $_validate = array(
    array('title','require','标题不能为空!'), //默认情况下用正则进行验证
    array('user','require','留言人不能为空!'),
    array('msg','require','内容不能为空!'),
  );
  protected $_auto = array (
    array('status','1'), // 新增的时候把status字段设置为1
    array('id','null'),
    array('admin_user','ms'),
    array('replay','null'),
    array('update_time','time',3,'function'), // 对update_time字段在更新的时候写入当前时间戳
    array('send_msg_time','time',3,'function'),
  );
}

视图  文件名 addmsg.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>title</title>
</head>
<body>
<div>
  <form action="__self__" method="post" >
    <table border="1" width="100%" class="table_a">
      <tr>
        <td>留言时间</td>
        <td><input type="text" name="update_time"/></td>
      </tr>
      <tr>
        <td>留言人</td>
        <td><input type="text" name="user" /></td>
      </tr>
      <tr>
        <td>标题</td>
        <td><input type="text" name="title" /></td>
      </tr>
      <tr>
        <td>内容</td>
        <td><input type="text" name="msg" /></td>
      </tr>
      <tr>
        <td>回复</td>
        <td><textarea name="replay"></textarea></td>
      </tr>
      <tr>
        <td colspan="2" align="center">
          <input type="submit" value="添加">
          <a href="__controller__/index" rel="external nofollow" rel="external nofollow" ><input type="button" value="返回"></a>
        </td>
      </tr>
    </table>
  </form>
</div>
</body>
</html>

视图  文件名 index.html

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>留言列表 -- hovertree</title>
  <style>
    .keleyitable {
      width: 800px;
    }
    .keleyitable table, td, th {
      border: 1px solid green;margin-top:10px;
    }
    .klytd {width:100px;text-align:right
    }
    .hvttd {
      width:500px}
  </style>
</head>
<body>
<div style="margin:0px auto;" class="keleyitable"><h2>留言列表</h2>
  <tr><td class="klytd"><a href="__controller__/addmsg" rel="external nofollow" >添加</a></td><td class="hvttd"></td></tr>
  <volist name="info" id="vo">
    <table>
      <tr><td class="klytd">留言时间:</td><td class="hvttd">{$vo.update_time|date="y-m-d h:i:s",###}</td></tr>
      <tr><td class="klytd">留言人:</td><td class="hvttd">{$vo.user}</td></tr>
      <tr><td class="klytd">标题:</td><td class="hvttd">{$vo.title}</td></tr>
      <tr><td class="klytd">内容:</td><td class="hvttd">{$vo.msg}</td></tr>
      <tr><td class="klytd">回复:</td><td class="hvttd">{$vo.replay}</td></tr>
    </table>
    <tr><td class="klytd"><a href="__controller__/upd/id/{$vo.id}" rel="external nofollow" >修改</a></td><td class="hvttd"></td></tr>
    <tr><td class="klytd"><a href="__url__/del/id/{$vo.id}" rel="external nofollow" >删除</a></td><td class="hvttd"></td></tr>
  </volist>
</div>
<div style="width:800px;margin:10px auto;font-family:arial, helvetica, sans-serif;text-align:center;">hovertree © 2014 keleyi.com </div>
<!--最近打算开发一个留言板,asp.net的开源项目,http://hovertree.codeplex.com -->
</body>
</html>

视图  文件名 sendmsg.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>title</title>
</head>
<body>
  <form action="" method="post">
    标   题: <input type="text" name="title"><span style="color:red;">{$errorinfo.title}</span><br><br>
    信   息: <input type="text" name="msg"><span style="color:red;">{$errorinfo.msg}</span><br><br>
    留言人: <input type="text" name="user"><span style="color:red;">{$errorinfo.user}</span><br><br>
    <input type="submit" value="提交">
  </form>
</body>
</html>

视图 文件名  upd.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>title</title>
</head>
<body>
<div>
  <form action="__self__" method="post" >
    <input type="hidden" name="id" value="{$info.id}">
    <table border="1" width="100%" class="table_a">
      <tr>
        <td>留言时间</td>
        <td><input type="text" name="update_time" value="{$info.update_time}" /></td>
      </tr>
      <tr>
        <td>留言人</td>
        <td><input type="text" name="user" value="{$info.user}" /></td>
      </tr>
      <tr>
        <td>标题</td>
        <td><input type="text" name="title" value="{$info.title}" /></td>
      </tr>
      <tr>
        <td>内容</td>
        <td><input type="text" name="msg" value="{$info.msg}" /></td>
      </tr>
      <tr>
        <td>回复</td>
        <td><textarea name="replay">{$info.replay}</textarea></td>
      </tr>
      <tr>
        <td colspan="2" align="center">
          <input type="submit" value="修改">
          <a href="__controller__/index" rel="external nofollow" rel="external nofollow" ><input type="button" value="返回"></a>
        </td>
      </tr>
    </table>
  </form>
  </div>
</body>
</html>

目录结构

PHP实现的简单留言板功能示例【基于thinkPHP框架】

数据库 sql语句

set sql_mode="no_auto_value_on_zero";
set time_zone = "+00:00";
/*!40101 set @old_character_set_client=@@character_set_client */;
/*!40101 set @old_character_set_results=@@character_set_results */;
/*!40101 set @old_collation_connection=@@collation_connection */;
/*!40101 set names utf8 */;
--
-- 資料庫: `msg`
--
-- --------------------------------------------------------
--
-- 表的結構 `ms_msg`
--
create table if not exists `ms_msg` (
 `id` int(10) not null auto_increment comment '主键',
 `admin_user` varchar(100) not null comment '管理员',
 `update_time` int(10) not null comment '更新时间',
 `status` int(2) not null comment '状态',
 `send_msg_time` int(10) not null comment '留言时间',
 `user` varchar(100) not null comment '留言人',
 `title` varchar(100) not null comment '标题',
 `msg` varchar(200) not null comment '内容',
 `replay` varchar(200) not null comment '回复',
 primary key (`id`)
) engine=myisam default charset=utf8 comment='留言表' auto_increment=19 ;
--
-- 轉存資料表中的資料 `ms_msg`
--
insert into `ms_msg` (`id`, `admin_user`, `update_time`, `status`, `send_msg_time`, `user`, `title`, `msg`, `replay`) values
(1, 'ms', 1479449110, 1, 1479449110, '1', '拉克丝的减肥', '对方科目了', 'null'),
(7, '', 321423432, 0, 0, 'kljflwk', 'kjsdfnlk', 'nlkdsjfn', 'kljnf'),
(3, 'ms', 1479451017, 1, 1479451017, '1', '轻松的发生我', '沃尔沃飞', 'null'),
(8, 'ms', 1479544687, 1, 1479544687, '', 'qwe', '', 'null'),
(9, 'ms', 1479544693, 1, 1479544693, 'qwe', 'qwe', 'qwe', 'null'),
(10, 'ms', 1479544970, 1, 1479544970, 'qwe', 'qwe', 'qwe', 'null'),
(11, 'ms', 1479544979, 1, 1479544979, '12', '12', '12', 'null'),
(12, 'ms', 1479545029, 1, 1479545029, '12', '12', '12', 'null'),
(13, 'ms', 1479546357, 1, 1479546357, '12', '12', '12', 'null'),
(14, 'ms', 1479547163, 1, 1479547163, '12', '12', '12', 'null'),
(16, 'ms', 1479547667, 1, 1479547667, '12', '12', '123', 'null'),
(17, 'ms', 2147483647, 1, 1479547682, '上来昆明3', '说的了付款', '蓝山咖啡', '123213');
/*!40101 set character_set_client=@old_character_set_client */;
/*!40101 set character_set_results=@old_character_set_results */;
/*!40101 set collation_connection=@old_collation_connection */;

更多关于thinkphp相关内容感兴趣的读者可查看本站专题:《thinkphp入门教程》、《thinkphp模板操作技巧总结》、《thinkphp常用方法总结》、《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《zend framework框架入门教程》及《php模板技术总结》。

希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。