用留言功能實現(xiàn)PbootCms文章評論
如果不需要在文章下顯示評論,到這里就結(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é)點個贊唄。
版權(quán)聲明:
本站所有文章和圖片均來自用戶分享和網(wǎng)絡(luò)收集,文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請勿用于商業(yè)用途,如果損害了您的權(quán)利,請聯(lián)系網(wǎng)站客服處理。