设为首页收藏本站关注微信<<< 抵制 IE6 人人有责 >>>
搜索
热搜: 活动 交友 discuz
查看: 1734|回复: 0
打印 上一主题 下一主题

[thinkPHP] thinkphp留言板实例

[复制链接]
跳转到指定楼层
楼主
发表于 2016-4-2 07:53:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
写个最简单的留言板来巩固巩固吧。最简单是多简单呢,简单到就是发表留言和显示留言,样式就直接用table拉出来。

第一步当然是创建thinkphp项目了,这个简单,照着手册完全没问题。

1,新建一个文件件,就取个拼音名吧,为: liuyan,即项目名称,并创建一个index.php的文件,我们称之为项目入口文件。

2,把ThinkPHP核心目录放到liuyan项目里面来,打开index.php文件,添加如下内容:
[AppleScript] 纯文本查看 复制代码
<?php
define('THINK_PATH','ThinkPHP/');
define('APP_NAME','liuyan');
define('APP_PATH','.');
require(THINK_PATH."ThinkPHP.php");
$App= new App();
$App->run();
?>


运行index.php即可,会帮我们创建好项目的目录结构。
今天就到这里了,休息!

thinkphp留言板(二)

      项目创建完后,lib文件夹下面的Action文件夹下面会生成一个IndexAction.class.php文件,里面会有一个IndexAction的类index就是操作IndexAction类的一个方法。这里我们称之为控制器。

      接下来就是开始写程序呢。首先确定思路,然后设计数据库and把界面拉出来,现在就用table直接套,不加任何样式。

一:

1):在Tpl文件下面的default文件夹下面建立一个index文件夹,在index文件夹里做界面。建立的这个index文件夹名必须与其控制器的名称相对应。

我们做网页一般分三部分,header,bottom,center

2)我们先把头做出来,建立一一个head.html文件,输入如下代码:
[AppleScript] 纯文本查看 复制代码
[mw_shl_code=applescript,true]<table width="562" border="1" align="center">
<tr>
<td colspan="2" align="center">清清留言板</td>
</tr>
<tr>
<td width="230" align="right"><a href="__URL__/index">浏览留言</a></td>
<td width="230" align="left"><a href="__URL__/add">发布留言</a></td>
</tr>
</table>
[/mw_shl_code]

效果:

暂且不管连接。
3)再做网页center部分。建立index.html文件,为浏览留言页面,输入如下代码:
[AppleScript] 纯文本查看 复制代码
<!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>
<include file="Index:head" />
<table width="562" height="118" border="1" align="center">
<tr>
    <td width="97" rowspan="2"> </td>
    <td width="286" height="36">主题:</td>
    <td width="59"><a href="mailto:waterjiang@yahoo.cn " target="_blank">邮箱</a></td>
    <td width="92"><A target="blank" href="tencent://message/?uin=274032290&Menu=yes"><IMG border="0" src="http://wpa.qq.com/pa?p=1:274032290:1" alt="有事点这里"></A></td>
</tr>
<tr>
    <td height="74" colspan="3"> </td>
</tr>
</table>
<include file="Index:bottom"/>

</body>
</html>



效果:
[AppleScript] 纯文本查看 复制代码
<include file="Index:head" />



相当于php中的include函数。

4)最后做一个网站底部bottom.html

[AppleScript] 纯文本查看 复制代码
<table width="562" border="1" align="center">
<tr>
    <td colspan="2" align="center">技术支持:清清工作室。版权所有:2009——2010 爱网网络公司</td>
</tr>
</table>



效果:



5)然后打开控制器IndexAction.class.php:

[AppleScript] 纯文本查看 复制代码
class IndexAction extends Action{
    public function index(){
   $this->display();
    }

}


运行index.phpl就可以看到就面效果了:


6)最后再做一个发布页面。add.html

[AppleScript] 纯文本查看 复制代码
<!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>
<include file="Index:head"/>
<form action="__URL__/insert/" method="post">
<table width="562" border="1" align="center">
<tr>
    <td width="155" rowspan="7">这里有时间加个选择图像的</td>
    <td width="88">留言人</td>
    <td width="297"><input type="text" name="name" id="name"/></td>
</tr>
<tr>
    <td>Email</td>
    <td><input type="text" name="email" /></td>
</tr>
<tr>
    <td>QQ</td>
    <td><input type="text" name="qq" /></td>
</tr>
<tr>
    <td>留言主题</td>
    <td><input type="text" name="title" /></td>
</tr>
<tr>
    <td>留言内容</td>
    <td><textarea name="con" cols="40" rows="5"></textarea></td>
</tr>
   <tr>
    <td>验证码</td>
    <td><input type="text" /><img src="__URL__/ma" /></td>
</tr>

<tr>
    <td colspan="2" align="center"><input type="submit" value=" 提 交 " />    <input type="reset" value=" 重 置 " /></td>
    </tr>
</table>
</form>
<include file="Index:bottom"/>
</body>
</html>




     前台搭建完了后自然是编码,写程序了。前台已经做出来了整系统个大思路肯定是出来了的。

1)数据库:
[SQL] 纯文本查看 复制代码
CREATE TABLE `u_liuyan` (
`id` int(10) NOT NULL auto_increment,
`name` varchar(20) character set utf8 collate utf8_bin NOT NULL,
`email` varchar(20) character set utf8 collate utf8_bin NOT NULL,
`qq` varchar(20) character set utf8 collate utf8_bin NOT NULL,
`title` varchar(45) NOT NULL,
`con` varchar(20) character set utf8 collate utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;



2)进行数据库操作。

1,配置数据库。

在conf文件夹下面建立一个config.php文件,填充:
[AppleScript] 纯文本查看 复制代码
<?php
return array(
"DB_TYPE" =>'mysql',
"DB_HOST" =>'localhost',
"DB_NAME" =>'liuyan',
"DB_USER" =>'root',
"DB_PWD" =>'',
"DB_PREFIX" =>'u_', //表的前缀
)
?>



2,数据操作

a)在LIb/Model下面建立一个 LiuyanModel.class.php 的文件夹,里面代码:
[AppleScript] 纯文本查看 复制代码
class LiuyanModel extends Model
{
}



这样以来我们就可以对数据进行操作了。

命名是:要操作的数据表名+Model.class.php。

b)在INdexAction.calss.php中新加入如下代码(蓝色为新加代码):

[AppleScript] 纯文本查看 复制代码
class IndexAction extends Action{
public function index(){
$this->display();
}

public function insert()
{
$User = D("Liuyan");
$User->create();
$User->add();
}

public function verify() //这个函数是成验证码的,和入库操作无关。
{
import("ORG.Util.Image");
Image::buildImageVerify();
}
}




这时回忆一下在add.html里面的表单提交的action="__URL__/insert",既是提交到这里insert()函数来处理。

这样,往数据里插入数据就已经实现,但很明显我们不能让用户任意玩数据库里插数据,还得进行数据过滤。

c),过滤数据。回到LiuyanModel.class.php,新加入代码:

[AppleScript] 纯文本查看 复制代码
class LiuyanModel extends Model
{
//过滤表单
protected $_validate = array(
array('name','require','留言人姓名必须!',1),
array('email','email ','邮箱格式错误','2'),
array('qq','number','请输入正确的QQ'),
array('title','require','主题不能为空',1),
array('con','require','内容不能为空',1),
array('ma','checkma','验证码错误',0,'callback'),

);

//验证验证码
public function checkma()
{
return md5($_POST['ma']) == $_SESSION['verify'];
}
}



thinkphp内置了数据对象的自动验证功能,需要使用系统的自动验证功能,只需要在Model类里面定义$_validate属性,支持的验证因子格式:

验证条件
EXISTS_TO_VAILIDATE 或者0 存在字段就验证(默认)
MUST_TO_VALIDATE 或者1 必须验证
VALUE_TO_VAILIDATE或者2 值不为空的时候验证

使用规则:手册里的数据验证有详细说明。

然后需要在INdexAction.class.php里进行一个报错操作。

[AppleScript] 纯文本查看 复制代码
class IndexAction extends Action{
public function index(){
$this->display();
}

public function insert()
{
$User = D("Liuyan");
if($User->create())
{
$User->add();
}else
{
  $this->error($User->getError());
}
}

public function verify()
{
import("ORG.Util.Image");
Image::buildImageVerify();
}
}



这样,发表留言就出来了。


然后就是我们要获取验证表单时的错误提示,我们需在default建立一个public文件夹,在在文件下面建立一个success.html文件。我们随便建一个表格吧。

[AppleScript] 纯文本查看 复制代码
<table width="299" border="0" align="center">
<tr>
<td align="center">{$error}</td> //获取错误提示
</tr>
<tr>
<td align="center">{$message}</td> //获取成功提示
  </tr>
<tr>
<td align="center">点击 <a href="{$jumpUrl}">这里</a> 跳转</td>//上一页
</tr>
</table>



这样我们在发布留言时,有验证错误就会提示错误。

这样,整个发布留言就完成了!

现在就是要吧数据取出来了,当然还得分页显现。
a):打开IndexAction.calss.php,在index函数里面修改为如下代码:

[AppleScript] 纯文本查看 复制代码
public function index(){
$xian=D("Liuyan");
$count = $xian->count('','id');//计算所有行
$listRows = '3';//每页显示的行数
$fields = 'id,name,email,qq,title,con';//需要查询哪些字段
import("ORG.Util.Page");//导入分页类 /ThinkPHP/lib/ORG/Util/Page.class.php
$p = new Page($count,$listRows);//通过类的构造函数来改变page的参数。$count为总数,$listrows为每一页的显示条目。
//设置查询参数。具体见“ThinkPHP/Lib/Think/Core/Model.class.php"1731行。
$list = $xian->findall('',$fields,'id desc',$p->firstRow.','.$p->listRows);

//模板输出
$page=$p->show();
$this->assign('list',$list);
$this->assign('page',$page);
$this->display();
}



b):打开index.html页面,

[AppleScript] 纯文本查看 复制代码
<!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>
<include file="Index:head" />
<volist name='list' id='vo'> //name 就是要显示的内容(控制器里发送过来的)与smarty模板相似,id相当于as,别名。
<table width="562" height="118" border="1" align="center">
<tr>
<td width="97" rowspan="2">留言人:{$vo.name}</td>
<td width="286" height="36">主题:{$vo.title}</td>
<td width="59"><a href="{$vo.email}" target="_blank">邮箱</a></td>
<td width="92"><A target="blank" href="tencent://message/?uin={$vo.qq}&Menu=yes"><IMG border="0" src="http://wpa.qq.com/pa?p=1:{$vo.qq}:1" alt="有事点这里"></A></td>
</tr>
<tr>
<td height="74" colspan="3">{$vo.con}</td>
</tr>
</table>

</volist>
<table width="562" height="30" border="1" align="center">
<tr>
<td align="center">{$page}</td>
</tr>
</table>
<include file="Index:bottom"/>

</body>
</html>



我们将看到效果:

这样留言板的基本功能就实现了。

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享淘帖
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

QQ|手机版|Archiver|源于生活(个人生活娱乐工作的笔记)css3,html5,学习笔记    

GMT+8, 2024-4-29 17:50 , Processed in 0.078000 second(s), 26 queries .

Powered by Mr.Kevin-ying

© 2004-2015

快速回复 返回顶部 返回列表