国产三级农村妇女在线,国产精品毛片a∨一区二区三区,国产乱子伦视频大全,国产精品色拉拉,国产欧美日韩一区二区三区,

首頁 > 技術(shù) > cms教程

用留言功能實現(xiàn)PbootCms文章評論

cms教程 2022-12-02 12:27:17
如果不需要在文章下顯示評論,到這里就結(jié)束啦。還可以多加幾個隱藏字段,記錄文章標(biāo)題,文章URL,方便查看。

教程肯定不能這么沒營養(yǎng)了。

我們使用message標(biāo)簽加載留言列表的時候,會把所有的留言都加載出來,再加上一個判斷articleid=={content:id},就實現(xiàn)了評論列表讀取。這個方法有個嚴(yán)重的BUG,就是分頁會不準(zhǔn)確。可能出現(xiàn)1頁都沒一條評論的情況。

優(yōu)化方案

那么我們需要對message標(biāo)簽進(jìn)行一個優(yōu)化,來更好的實現(xiàn)評論列表效果。

優(yōu)化后的message標(biāo)簽如下,通過filter屬性來過濾出我們所需的留言(評論)。

{pboot:message num=10 filter=articleid|{content:id} page=1}
{/pboot:message}

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

為了突出重點,代碼僅貼了主要部分,看我的注釋@cms88,小朋友請自行整合。

    // 解析留言板標(biāo)簽
    public function parserMessageLabel($content)
    {
        $pattern = '/{pboot:message(s+[^}]+)?}([sS]*?){/pboot:message}/';
        $pattern2 = '/[message:([w]+)(s+[^]]+)?]/';
        if (preg_match_all($pattern, $content, $matches)) {
            $count = count($matches[0]);
            for ($i = 0; $i < $count; $i ++) {
                // 獲取調(diào)節(jié)參數(shù)
                $params = $this->parserParam($matches[1][$i]);
                $num = $this->config('pagesize');
                $page = true;
                $start = 1;
                $lg = '';
                $filter = '';
                
                foreach ($params as $key => $value) {
                    ......
                }
                
                // 起始數(shù)校驗
                if (! is_numeric($start) || $start < 1) {
                    $start = 1;
                }

                // filter數(shù)據(jù)篩選 @cms88
                $where = array();
                if ($filter) {
                    $filter = explode('|', $filter);
                    $where = $filter[0] . "='" . escape_string($filter[1]) . "'";
                }
                
                // 讀取數(shù)據(jù) @cms88
                if (! $data = $this->model->getMessage(escape_string($num), $page, $start, $lg, $where)) {
                    $content = str_replace($matches[0][$i], '', $content);
                    continue;
                }
                
                // 匹配到內(nèi)部標(biāo)簽
                if (preg_match_all($pattern2, $matches[2][$i], $matches2)) {
                    $count2 = count($matches2[0]); // 循環(huán)內(nèi)的內(nèi)容標(biāo)簽數(shù)量
                } else {
                    $count2 = 0;
                }
                
                $out_html = '';
                $key = 1;
                foreach ($data as $value) { // 按查詢數(shù)據(jù)條數(shù)循環(huán)
                    ......
                }
                $content = str_replace($matches[0][$i], $out_html, $content);
            }
        }
        return $content;
    }

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

// 獲取留言
    public function getMessage($num, $page = true, $start = 1, $lg = null, $filter = null)   //@cms88 增加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();
        }
    }

至此,功能實現(xiàn)。學(xué)會的同學(xué)點個贊唄。

TAg

加載中~

本網(wǎng)站LOGO受版權(quán)及商標(biāo)保護(hù),版權(quán)登記號:國作登字-2022-F-10126915,未經(jīng)湖南木星科技官方許可,嚴(yán)禁使用。
Copyright ? 2012-2022 湖南木星科技有限公司(木星網(wǎng))版權(quán)所有
轉(zhuǎn)載內(nèi)容版權(quán)歸作者及來源網(wǎng)站所有,本站原創(chuàng)內(nèi)容轉(zhuǎn)載請注明來源,商業(yè)媒體及紙媒請先聯(lián)系:aishangyiwan@126.com

工信部備案號:湘ICP備19012813號-5