前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用留言功能实现PbootCms文章评论

用留言功能实现PbootCms文章评论

原创
作者头像
阿哲
发布2022-03-17 17:20:44
1.1K0
发布2022-03-17 17:20:44
举报
文章被收录于专栏:阿哲学习笔记

前言

废话不多说,今天上一个用PbootCms留言板实现文章留言的功能。

操作思路

给留言板添加字段,例如叫:articleid

然后在文章下面加入留言表单。

在表单中增加一个隐藏字段:

代码语言:javascript
复制
<!--用来记录文章的ID-->
<input type="hidden" value="{content:id}" name="articleid">

如果不需要在文章下显示评论,到这里就结束啦。还可以多加几个隐藏字段,记录文章标题,文章URL,方便查看。

那教程肯定不能这么没营养了。

我们使用message标签加载留言列表的时候,会把所有的留言都加载出来,再加上一个判断articleid=={content:id},就实现了评论列表读取。这个方法有个严重的BUG,就是分页会不准确。可能出现1页都没一条评论的情况。

优化方案

那么我们需要对message标签进行一个优化,来更好的实现评论列表效果。

优化后的message标签如下,通过filter属性来过滤出我们所需的留言(评论)。

代码语言:javascript
复制
{pboot:message num=10 filter=articleid|{content:id} page=1}
{/pboot:message}

修改文件位置1:/apps/home/controller/ParserController.php,大约在1866行,找到parserMessageLabel方法

代码语言:javascript
复制
    // 解析留言板标签
    public function parserMessageLabel($content)
    {
        $pattern = '/\{pboot:message(\s+[^}]+)?\}([\s\S]*?)\{\/pboot:message\}/';
        $pattern2 = '/\[message:([\w]+)(\s+[^]]+)?\]/';
        if (preg_match_all($pattern, $content, $matches)) {
            $count = count($matches[0]);
            for ($i = 0; $i < $count; $i ++) {
                // 获取调节参数
                $params = $this->parserParam($matches[1][$i]);
                $num = $this->config('pagesize');
                $page = true;
                $start = 1;
                $lg = '';
                $filter = '';
                
                foreach ($params as $key => $value) {
                    ......
                }
                
                // 起始数校验
                if (! is_numeric($start) || $start < 1) {
                    $start = 1;
                }

                // filter数据筛选 
                $where = array();
                if ($filter) {
                    $filter = explode('|', $filter);
                    $where = $filter[0] . "='" . escape_string($filter[1]) . "'";
                }
                
                // 读取数据 
                if (! $data = $this->model->getMessage(escape_string($num), $page, $start, $lg, $where)) {
                    $content = str_replace($matches[0][$i], '', $content);
                    continue;
                }
                
                // 匹配到内部标签
                if (preg_match_all($pattern2, $matches[2][$i], $matches2)) {
                    $count2 = count($matches2[0]); // 循环内的内容标签数量
                } else {
                    $count2 = 0;
                }
                
                $out_html = '';
                $key = 1;
                foreach ($data as $value) { // 按查询数据条数循环
                    ......
                }
                $content = str_replace($matches[0][$i], $out_html, $content);
            }
        }
        return $content;
    }

修改位置2:/apps/home/model/ParserModel.php,大约在723行,getMessage方法。

代码语言:javascript
复制
// 获取留言
    public function getMessage($num, $page = true, $start = 1, $lg = null, $filter = null)   // 增加filter
    {
        if ($lg == 'all') {
            $where = array();
        } elseif ($lg) {
            $where = array(
                'acode' => $lg
            );
        } else {
            $where = array(
                'acode' => get_lg()
            );
        }
        if ($page) {
            return parent::table('ay_message')->where("status=1")
                ->where($where)
                ->where($filter, 'OR')
                ->order('id DESC')
                ->decode(false)
                ->page(1, $num, $start)
                ->select();
        } else {
            return parent::table('ay_message')->where("status=1")
                ->where($where)
                ->where($filter, 'OR')
                ->order('id DESC')
                ->decode(false)
                ->limit($start - 1, $num)
                ->select();
        }
    }

至此,功能实现。学会的同学点个赞呗。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 操作思路
  • 优化方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档