Thinkphp留言板模块Demo

首先在在Mysql数据库中新建一个数据库,配置下Thinkphp数据库链接,详细就不说明了,创建一个表,新建三个字段,id,name,guestbook,其中id为自增字段且为唯一主键,也可以通过下面代码执行sql语句:

CREATE TABLE IF NOT EXISTS `0535coder_guest` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(2000) NOT NULL,
  `guest` varchar(2000) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
 
--
-- 转存表中的数据 `joys_guest`
--
 
INSERT INTO `0535coder_guest` (`id`, `name`, `guest`) VALUES
(1, 'name', 'guest'),
(2, 'lida', 'dage'),
(4, 'yueyue', 'dijiangyue'),
(5, 'xiaobai', 'wangjing'),
(11, 'test', 'test'),
(7, 'wxz', 'wwzzxx1233'),
(13, 'demo', 'demo');

其次是创建一个类文件UserAction.class.php 文件代码如下:

<?php
class GuestAction extends CommonAction{
public function index(){
$guest = M('Guest');
$arr = $guest->order('id')->select();
$this->assign('guestdate',$arr);
$this->display();
//echo '查询留言';
}
Public function displayadd(){
$guest = M('Guest');
$this->display();
//添加留言页面
}
public function guestadd(){
$guest = M('Guest');
$guest->id=$_POST['id'];
$guest->name=$_POST['name'];
$guest->guest=$_POST['guestbook'];
$count=$guest->add();
if($count>0){
$this->success('添加数据成功','__URL__');
 
}
else{
$this->error('添加数据失败');
}
$this->display();
//添加留言处理程序
}
 
public function guestdel(){
$guest = M('Guest');
$id=$_GET['id'];
$count = $guest->delete($id);
if($count>0){
$this->success('数据删除成功!');
}else{
$this->error('数据删除失败!');
}
//删除留言
}
 
public function guestup(){
$guest = M('Guest');
$id=$_GET['id'];
$arr=$guest->find($id);
$this->assign('data',$arr);
$this->display();
//修改留言-显示
}
 
Public function guestupload(){
$guest = M('Guest');
$data['id']=$_POST['id'];
$data['name']=$_POST['name'];
$data['guest']=$_POST['guestbook'];
$count=$guest->save($data);
//var_dump($data);
if($count>0){
$this->success('数据修改成功!','__URL__');
}
else{
$this->error('数据修改失败');
}
//修改留言-添加数据库
}
}
?>

剩下的就是模版文件了,这里比较简单的一个表格,分为三个文件,依次是:

1.index.html 文件内容为:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言板查询页面</title>
</head>
<body>
<table width="75%" border="1" align="center">
  <tr>
    <td>唯一ID</td>
    <td>网名</td>
    <td>留言内容</td>
<td><a href='__URL__/guestup'>修改留言</a> </td>
<td><a href='__URL__/guestdel'>删除留言</a></td>
  </tr>
  <volist name='guestdate' id='vo'>
  <tr>
    <td><{$vo['id']}></td>
    <td><{$vo['name']}></td>
    <td><{$vo['guest']}></td>
<td><a href="__URL__/guestup/id/<{$vo['id']}>">修改留言</a> </td>
<td><a href="__URL__/guestdel/id/<{$vo['id']}>">删除留言</a></td>
  </tr>
  </volist>
</table>
<p><a href='__URL__/displayadd'>添加新留言</a></p>
</body>
</html>

2.guestup.html文件内容为:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言板修改页面</title>
</head>
<body>
<form action='__URL__/guestupload/id/<{$data.id}>' method='post'>
  <p> ID:
    <input type="text" name="id" value="<{$data.id}>"/>
  </p>
  <p> 网名:
    <input type="text" name="name" value="<{$data.name}>" />
  </p>
  <p>留言:
    <textarea name="guestbook"><{$data.guest}></textarea>
  </p>
  <p>
    <input type="submit" name="Submit" value="提交" />
  </p>
</form>
<p></p>
</body>
</html>

3.displayadd.html文件内容为:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言板增加页面</title>
</head>
<body>
<form action='__URL__/guestadd/' method='post'>
  <p> ID:
    <input type="text" name="id" />
  </p>
  <p> 网名:
    <input type="text" name="name" />
  </p>
  <p>留言:
    <textarea name="guestbook"></textarea>
  </p>
  <p>
    <input type="submit" name="Submit" value="提交" />
  </p>
</form>
<p></p>
</body>
</html>

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.