200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > php 使用tp框架写留言板 基于thinkPHP框架实现留言板的方法

php 使用tp框架写留言板 基于thinkPHP框架实现留言板的方法

时间:2024-09-12 00:20:43

相关推荐

php 使用tp框架写留言板 基于thinkPHP框架实现留言板的方法

这篇文章主要介绍了基于thinkPHP框架实现留言板的方法,简单分析了thinkPHP框架实现留言板的流程及控制器与模型的相关核心代码,需要的朋友可以参考下

本文实例讲述了基于thinkPHP框架实现留言板的方法。分享给大家供大家参考,具体如下:

奋斗了一天,终于THINKPHP小邓留言版的概念版出来了

其实真的THINKPHP开发速度很快,作为一个互联网上“搬砖”的,从事这种 纯码农的事也是无可厚非的。

代码就实现了如下功能

1.留言功能。

2.验证功能。

3.分页显示功能。

就是写了几行代码(PS:页面设计代码不算,就算控制器和模型的代码)

下面我公布一下控制的器的代码,关于THINKPHP的代码规则我就不阐述了,看thinkphp手册就可以了。

class IndexAction extends Action

{

public function index() {

$Form = M("word");

// 按照id排序显示前6条记录

import("@.ORG.Page"); //导入分页类

$count = $Form->count(); //计算总数

$p = new Page ( $count, 1 );

$list=$Form->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();

$page = $p->show ();

$this->assign ( "page", $page );

$this->assign ( "list", $list );

$this->display(); //模板调用,这个是关键。

}

//数据插入

public function insert() {

$word = D("word");

if($vo = $word->create())

{

if(false !== $word->add())

{

$this->success("数据添加成功");

}

else

{

$this->error('数据写入错误!');

}

}

else

{

$this->error($word->getError());

}

}

//验证重复

public function checkTitle()

{

if (!empty($_POST['username'])) {

$Form = M("word");

//getByTitle是model的获取数据根据某字段获取记录的魔术方法

//比如getById etc getByXXX XXX大写

if ($Form->getByUsername($_POST['username'])) {

$this->error('标题已经存在');

} else {

$this->success('标题可以使用!');

}

} else {

$this->error('标题必须');

}

}

}

下面是验证模型的代码

class wordModel extends Model{

protected $_validate = array(

array('username', 'require', '称呼必须!', 1),//1为必须验证

array('email', 'email', '邮箱格式错误!', 2),//2为不为空时验证

array('qq','number','QQ号错误',2),

array('content', 'require', '内容必须',1),

array('username','','称呼已经存在',0,'unique',1)

);

protected $_auto = array(

array('datetime', 'get_date',1, 'callback'),

array('ip','getip',1,'callback')

);

protected function get_date()

{

return date("Y-m-d H:i:s");

}

protected function getip()

{

return $_SERVER['REMOTE_ADDR'];

}

}

thinkphp有一个要注意的,在CURD操作中,都规定要用表名。

相关推荐:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。