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

2018-09-07 19:13

阅读:419

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

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

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

  代码就实现了如下功能

  1.留言功能。

  2.验证功能。

  3.分页显示功能。

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

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

   class IndexAction extends Action { public function index() { $Form = M(word); // 按照id排序显示前6条记录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(标题必须); } } }

  下面是验证模型的代码

   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相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《smarty模板入门基础教程》及《PHP模板技术总结》。

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


评论


亲,登录后才可以留言!