基于thinkPHP框架实现留言板的方法

2025-05-29 0 43

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

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

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

代码就实现了如下功能

1.留言功能。

2.验证功能。

3.分页显示功能。

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50
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('<font color=red>标题已经存在</font>');

} else {

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

}

} else {

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

}

}

}

下面是验证模型的代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
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操作中,都规定要用表名。

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 基于thinkPHP框架实现留言板的方法 https://www.kuaiidc.com/96815.html

相关文章

发表评论
暂无评论