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:342756browse

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(&#39;<font color=red>标题已经存在</font>&#39;);
      } else {
        $this->success(&#39;标题可以使用!&#39;);
      }
    } else {
      $this->error(&#39;标题必须&#39;);
    }
  }
}

The following is the code to verify the model

class wordModel extends Model{
  protected $_validate = array(
   array(&#39;username&#39;, &#39;require&#39;, &#39;称呼必须!&#39;, 1),//1为必须验证
   array(&#39;email&#39;, &#39;email&#39;, &#39;邮箱格式错误!&#39;, 2),//2为不为空时验证
   array(&#39;qq&#39;,&#39;number&#39;,&#39;QQ号错误&#39;,2),
   array(&#39;content&#39;, &#39;require&#39;, &#39;内容必须&#39;,1),
   array(&#39;username&#39;,&#39;&#39;,&#39;称呼已经存在&#39;,0,&#39;unique&#39;,1)
  );
  protected $_auto = array(
   array(&#39;datetime&#39;, &#39;get_date&#39;,1, &#39;callback&#39;),
   array(&#39;ip&#39;,&#39;getip&#39;,1,&#39;callback&#39;)
  );
  protected function get_date()
  {
   return date("Y-m-d H:i:s");
  }
  protected function getip()
  {
   return $_SERVER[&#39;REMOTE_ADDR&#39;];
  }
}

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