Method to implement message board based on thinkPHP framework_php example

WBOY
Release: 2023-03-03 06:26:01
Original
1390 people have browsed it

The example in this article describes how to implement a message board based on the thinkPHP framework. Share it with everyone for your reference, the details are as follows:

After struggling for a day, the concept version of THINKPHP Xiao Deng’s message board is finally out

In fact, the development speed of THINKPHP is very fast. As a "brick mover" on the Internet, it is understandable to 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)

I will publish the code of the controller below. I will not elaborate on the code rules of THINKPHP. 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('<font color=red>标题已经存在</font>');
      } else {
        $this->success('标题可以使用!');
      }
    } else {
      $this->error('标题必须');
    }
  }
}

Copy after login

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'];
  }
}

Copy after login

Thinkphp has one thing to note. In CURD operations, table names are required.

Readers who are interested in more thinkPHP related content can check out the special topics of this site: "ThinkPHP Getting Started Tutorial", "ThinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "Smarty Template Basic Tutorial" and "PHP Template Technology" Summarize".

I hope that what is described in this article will be helpful to everyone’s PHP program design based on the ThinkPHP framework.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!