Home  >  Article  >  Backend Development  >  Method to implement message board based on thinkPHP framework

Method to implement message board based on thinkPHP framework

不言
不言Original
2018-05-05 15:25:342624browse

This article mainly introduces the method of implementing a message board based on the thinkPHP framework. It briefly analyzes the process of implementing the message board in the thinkPHP framework and the related core code of the controller and model. Friends in need can refer to it

The example in this article describes the method of implementing a message board based on the thinkPHP framework. I would like to share it with you for your reference. The details are as follows:

After struggling for a day, the concept version of THINKPHP Xiao Deng’s message board was finally released.

In fact, THINKPHP is really developing very quickly. As an Internet user, There is nothing wrong with those who "move bricks" and engage in this kind of pure code farmer's work.

The code implements the following functions

1. Message function.

2. Verification function.

3. Paging display function.

Just wrote a few lines of code (PS: The page design code does not count, even the controller and model code)

Now I will publish the code of the controller, about THINKPHP I won’t elaborate on the coding rules, just read the thinkphp manual.

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('标题必须');
    }
  }
}

The following is the code to verify the model

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 has one thing to pay attention to , in CURD operations, table names are required.

Related recommendations:

Build OAuth20 service based on ThinkPHP framework

The above is the detailed content of Method to implement message board based on thinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn