Home Backend Development PHP Tutorial ThinkPHP无限级分类原理实现留言与回复功能实例_PHP

ThinkPHP无限级分类原理实现留言与回复功能实例_PHP

May 31, 2016 pm 07:28 PM
thinkphp message

ThinkPHP

本文所述留言板程序使用了无限级分类的原理,可以实现无限级留言与回复。留言列表gclist保留了留言层次空格,使留言--回复层次分明。分享给大家供大家参考。具体分析如下:

功能上,本程序可以实现无限级留言与回复,即对留言回复,对回复的留言回复。当然你也可以作有限制的控制,使其只对留言回复,关键是在模板代码中去掉回复的留言中的“回复该留言”即可。欢迎去拍砖!

程序效果如下图所示:

完整源码点击此处本站下载。

数据表:

代码如下:

-- ----------------------------    
-- Table structure for `wb_guestbook`    
-- ----------------------------    
DROP TABLE IF EXISTS `wb_guestbook`;    
CREATE TABLE `eway_guestbook` (    
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,    
  `pid` int(10) NOT NULL,    
  `email` varchar(50) NOT NULL,    
  `path` varchar(100) NOT NULL,    
  `username` varchar(30) NOT NULL,    
  `updatetime` int(10) NOT NULL,    
  `ip` varchar(15) NOT NULL,    
  `url` varchar(200) NOT NULL,    
  `inputtime` int(10) NOT NULL,    
  `content` text NOT NULL,    
  `verify` varchar(32) NOT NULL,    
  `isreply` tinyint(1) NOT NULL,    
  `status` tinyint(1) NOT NULL,    
  PRIMARY KEY (`id`)    
) ENGINE=MyISAM AUTO_INCREMENT=42 DEFAULT CHARSET=utf8;

代码:

代码如下:

// +----------------------------------------------------------------------    
// | WBlog    
// +----------------------------------------------------------------------    
// | Copyright (c) 2008  http://www.w3note.com All rights reserved.    
// +----------------------------------------------------------------------    
// | Author: 网菠萝果    
// +----------------------------------------------------------------------    
// $Id$    
/**    
 +------------------------------------------------------------------------------    
 * @class 留言板控制器GuestbookAction.class.php    
 +------------------------------------------------------------------------------    
 */
class GuestbookAction extends CommonAction {    
    public function index(){    
        $garr= D('Guestbook')->gclist("id,username,inputtime,pid,url,content,path,concat(path,'-',id) as bpath");    
                 
        $this->assign('Gklist', $garr['list']);    
        $this->assign('page',$garr['page']);    
        $this->display();    
    }    
// +----------------------------------------------------------------------    
// | 添加留言    
// +----------------------------------------------------------------------    
                 
    public function add(){    
        $this->adddata('Guestbook');    
                         
        }    
// +----------------------------------------------------------------------    
// | 网址跳转。如在表单url添加网址的话,点击会跳转到相关网站    
// +----------------------------------------------------------------------    
         
    public function tourl(){    
      $this->gettourl('Guestbook');    
      }     
}    
?>    
// +----------------------------------------------------------------------    
// | WBlog    
// +----------------------------------------------------------------------    
// | Copyright (c) 2008   http://www.w3note.com All rights reserved.    
// | Author: 网菠萝果    
// +----------------------------------------------------------------------    
// $Id$    
/**    
 +------------------------------------------------------------------------------    
 * @function 留言板模型 类GuestbookModel.class.php   
 +------------------------------------------------------------------------------    
 */
         
class GuestbookModel extends RelationModel{    
// +----------------------------------------------------------------------    
// | $_validate表单自动验证    
// +----------------------------------------------------------------------    
         
     protected $_validate  = array(    
                array('email','require','请填写您的邮箱!'),    
                array('email','email','邮箱格式错误!'),     
                         
               );    
// +----------------------------------------------------------------------    
// | $_auto表单自动填充    
// +----------------------------------------------------------------------    
                  
        protected $_auto=array(    
                 array('status','1'),      
                 array('inputtime','time',1,'function'),    
                 array('content','content',1,'callback'),    
                 array('url','geturl',1,'callback'),                    
                 array ('inputtime','time',1,'function'),    
                 array('path','path',3,'callback'),     
                 array('username','getusername',3,'callback'),                         
                   );       
// +----------------------------------------------------------------------    
// | getusername()过滤用户名    
// +----------------------------------------------------------------------            
      public function getusername(){    
          if (isset ($_POST['username'])) {    
            if(trim($_POST['username'])=='网菠萝果'){    
                return $data= ' ̄□ ̄';        
            }elseif(strlen($_POST['username']) >10){                 
                return $data= msubstr($_POST['username'],0,5);    
            }else{    
                return $data= $_POST['username'];    
            }    
        }       
        }     
// +----------------------------------------------------------------------    
// | path()返回子类的path,父类的path的值为0    
// +----------------------------------------------------------------------      
     public function path(){    
           $pid=isset($_POST['pid'])?(int)$_POST['pid']:0;    
           $id=$_POST['id'];    
            if($pid==0){                    
                return 0;    
            }    
                     
            $fat=$this->where(array('id' => $pid))->find();    
            $data=$fat['path'].'-'.$fat['id'];              
            return $data;    
        }    
// +----------------------------------------------------------------------    
// | content()过滤留言内容    
// +----------------------------------------------------------------------            
    public function content() {    
        if (isset ($_POST['content']) && !empty ($_POST['content'])) {    
             $data =deleteHtmlTags($_POST['content']);    
             $data =safeHtml($data);    
            if (strlen($data) > 1000) {    
                $data = msubstr($data, 0, 500);    
            }    
            return $data;    
          }    
           }    
 // +----------------------------------------------------------------------    
// | content()过滤URL    
// +----------------------------------------------------------------------                
    public function geturl(){    
        if (isset ($_POST['url'])) {    
        $data = deleteHtmlTags($_POST['url']);    
        $data = safeHtml($data);    
            return $data=$data?$data:"";    
        }    
    }       
// +----------------------------------------------------------------------    
// |gclist($field,$where='',$pagesize=30)留言列表    
// +----------------------------------------------------------------------    
// |$field,字段    
// +----------------------------------------------------------------------    
// |$where查询条件,默认为空    
// +----------------------------------------------------------------------    
// |$pagesize分页记录,默认为30     
// +----------------------------------------------------------------------    
// |使用方法,看上面的控制器调用    
// +----------------------------------------------------------------------    
         
     public function gclist($field,$where='',$pagesize=30) {    
        import("ORG.Util.Page");    
         $count = $this->field('id')->where($where)->count();    
         $P = new Page($count, $pagesize);    
                  
        $list=$this->field($field)->where($where)->order('bpath,id')->limit($P->firstRow . ',' . $P->listRows)->select();    
         
        foreach ($list as $k => $v) {    
            $list[$k]['count'] = count(explode('-', $v['bpath']));    
            $list[$k]['tousername']=$this->where(array('id'=> $v['pid']))->getField('username');    
            $str = '';    
            if ($v['pid'] 0) {    
                for ($i = 0; $i                     $str .= ' ';    
                }    
                $str .= ' ';    
            }    
            $list[$k]['space'] = $str;    
        }    
        $P->setConfig('header', '篇');    
        $P->setConfig('prev', "«");    
        $P->setConfig('next', '»');    
        $P->setConfig('first', '|«');    
        $P->setConfig('last', '»|');    
        $page = $P->show();    
        $arr=array('page'=>$page,'list'=>$list);    
        return $arr;    
    }    
}    
?>

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

How to leave a message on Xianyu How to leave a message on Xianyu and chat privately How to leave a message on Xianyu How to leave a message on Xianyu and chat privately Mar 23, 2024 am 09:10 AM

Xianyu APP is a super second-hand trading software. It allows everyone to choose products here at will, or publish their own products for sale. There is no problem. Everything can be realized here. When you use it here When using the Xianyu APP, everyone needs to communicate in a timely manner by leaving messages to get more information and help you sell or buy goods better. It is relatively convenient and is aimed at those who still don’t know how to leave messages on Xianyu. For my friends, I have now brought you specific Xianyu message tutorials. I hope it will be helpful to you. Xianyu message tutorial: 1. First, open Xianyu. 2. Then enter the interface and click on a product. 3. Then in the interface that appears, slide up and click

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

See all articles