婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > php無限級評論嵌套實現(xiàn)代碼

php無限級評論嵌套實現(xiàn)代碼

熱門標簽:房產(chǎn)中介用的是什么外呼系統(tǒng) 遼寧ai電銷機器人價格 上海做外呼線路的通信公司 地圖標注專員怎么樣 福建銀行智能外呼系統(tǒng)價格 四川保險智能外呼系統(tǒng)供應商 寧波外呼營銷系統(tǒng) 長沙做地圖標注公司 電話機器人銷售主要負責什么

我在設(shè)計BB的過程中,也一直在思考是否可以不通過遞歸來實現(xiàn)無限級分類的結(jié)構(gòu)展現(xiàn)和父子結(jié)構(gòu)查找,因為如果不對這里的算法進行優(yōu)化后果可能是致命的!試想一下,一篇文章如果評論數(shù)為300,按正常的遞歸算法,至少就得查詢數(shù)據(jù)庫301次,而且還是在沒有任何嵌套的情況下,如果有過一兩級嵌套或者評論數(shù)過1000,那數(shù)據(jù)庫不是直接宕掉?
而實際上,PHP強大的數(shù)組處理能力已經(jīng)能幫助我們快速方便的解決這個問題。下圖為一個無限級分類的

數(shù)據(jù)庫結(jié)構(gòu):

IDparentID newsID commts
108文章ID為8的評論
21 8對ID為1的評論的回復
328對ID為2的評論的回復

要在前臺嵌套式的展現(xiàn)文章編號8的評論,其實我們只用查詢一次數(shù)據(jù)庫,即“SELECT * FROM TABLE WHERE newsID=8”,而把后期的遞歸工作交給強大的PHP數(shù)組來完成。這里可能涉及的問題就是數(shù)組的結(jié)構(gòu)關(guān)系的重組,即將所有停留在一級分類上的評論全部放到自己的parentID下,形成children項。
下面將BBComment類中這塊的代碼粘貼出來,希望與大家分享下我的思路,也希望大家能夠提出更好更有效率的算法。

方法一

/** 
 * 按ID條件從評論數(shù)組中遞歸查找 
 * 
 */ 
function getCommentsFromAryById($commtAry, $id) 
{ 
 if ( !is_array($commtAry) ) return FALSE; 
 foreach($commtAry as $key=>$value) { 
  if ( $value['id'] == $id ) return $value; 
  if ( isset($value['children'])  is_array($children) ) $this->getCommentsFormAryById($value['children'], $id); 
 } 
} 
/** 
 * 追加 子評論 到 主評論 中,并形成children子項 
 * 
 * @param array $commtAry 原評論數(shù)據(jù)引用 
 * @param int $parentId 主評論ID 
 * @param array $childrenAry 子評論的值 
 */ 
function addChildenToCommentsAry($commtAry, $parentId, $childrenAry) 
{ 
 if ( !is_array($commtAry) ) return FALSE; 
 
 foreach($commtAry as $key=>$value) { 
  if ( $value['id'] == $parentId ) { 
   $commtAry[$key]['children'][] = $childrenAry; 
   return TRUE; 
  } 
  if ( isset($value['children']) ) $this->addChildenToCommentsAry($commtAry[$key]['children'], $parentId, $childrenAry); 
 } 
} 
 $result = $this->BBDM->select($table, $column, $condition, 0, 1000); 
 
 /* 開始進行嵌套評論結(jié)構(gòu)重組 */ 
 array_shift($result); 
 $count = count($result); 
 $i  = 0; 
 while( $i$count ) { 
  if ( '0' != $result[$i]['parentId'] ) { 
   $this->addChildenToCommentsAry($result, $result[$i]['parentId'], $result[$i]); 
   unset($result[$i]); 
  } 
  $i++; 
 } 
 $result = array_values($result); 
 /* 重組結(jié)束 */ 

實現(xiàn)方法二

核心代碼摘自WordPress

?php
$comments = array (
  array (
    'id' => '3',
    'parent' => '0'
  ),
  array (
    'id' => '9',
    'parent' => '0'
  ),
  array (
    'id' => '1',
    'parent' => '3'
  ),
  array (
    'id' => '2',
    'parent' => '3'
  ),
  array (
    'id' => '5',
    'parent' => '1'
  ),
  array (
    'id' => '7',
    'parent' => '1'
  )
);
function html5_comment($comment) {
  echo 'li>';
  echo 'id:', $comment['id'], ' parent:', $comment['parent'];
}
function start_el( $output, $comment) {
  ob_start();
  html5_comment($comment);
  $output .= ob_get_clean();
}
function end_el( $output) {
  $output .= "/li>!-- #comment-## -->\n";
}
function start_lvl( $output) {
  $output .= 'ol class="children">' . "\n";
}
function end_lvl( $output) {
  $output .= "/ol>!-- .children -->\n";
}
function display_element($e,  $children_elements, $max_depth, $depth,  $output) {
  $id = $e['id'];
  start_el($output, $e); //當前評論的開始代碼
  if ($max_depth > $depth +1  isset ($children_elements[$id])) { //如果沒超過最大層,并且存在子元素數(shù)組
    foreach ($children_elements[$id] as $child) {
      if (!isset ($newlevel)) { //第一次循環(huán)沒設(shè)置變量$newlevel,所以把$newlevel設(shè)為true,并且開始子元素的開始代碼;第二次及之后的循環(huán),已經(jīng)設(shè)置了$newlevel,就不會再添加子元素的開始代碼。因為同一批循環(huán)時兄弟元素,所以只需要一個子元素開始代碼,循環(huán)內(nèi)容為并列關(guān)系。
        $newlevel = true;
        start_lvl($output);
      }
      display_element_template($child, $children_elements, $max_depth, $depth +1, $output); //$child作為參數(shù),繼續(xù)去尋找下級元素
    }
    unset ($children_elements[$id]); //用完釋放變量,以后就不會重復判斷該值了,遞歸后繼續(xù)判斷剩下的子元素
  }
  if (isset ($newlevel)  $newlevel) { //如果前面找到了子元素,這里就要執(zhí)行子元素的結(jié)束代碼
    end_lvl($output);
  }
  end_el($output); //當前評論的結(jié)束代碼
}
function display_element_template($e,  $children_elements, $max_depth, $depth,  $output) {
  $id = $e['id'];
  display_element($e, $children_elements, $max_depth, $depth, $output);
  if ($max_depth = $depth +1  isset ($children_elements[$id])) { //如果超出最大層級,并且子元素存在的話,以$child為參數(shù)繼續(xù)往下找
    foreach ($children_elements[$id] as $child) {
      display_element_template($child, $children_elements, $max_depth, $depth, $output);
    }
    unset ($children_elements[$id]); //用完釋放變量
  }
}
function comments_list($comments) {
  $top_level_elements = array ();
  $children_elements = array ();
  foreach ($comments as $e) {
    if (0 == $e['parent']) {
      $top_level_elements[] = $e;
    } else {
      $children_elements[$e['parent']][] = $e;
    }
  }
  $output = '';
  foreach ($top_level_elements as $e) {
    display_element_template($e, $children_elements, 2, 0, $output);
  }
  //var_dump($children_elements);//由于每次用完$children_elements后都會釋放變量,所以到最后$children_elements為空數(shù)組
  return $output;
}
echo 'ol class="comment-list">', comments_list($comments), '/ol>';

這篇文章就介紹到這了,其實大家多參考一些開源的cms也可以看到很多不錯的代碼,希望大家以后多多支持腳本之家

您可能感興趣的文章:
  • php利用嵌套數(shù)組拼接與解析json的方法
  • PHP中實現(xiàn)MySQL嵌套事務的兩種解決方案
  • PHP中的函數(shù)嵌套層數(shù)限制分析
  • PHP 修復未正常關(guān)閉的HTML標簽實現(xiàn)代碼(支持嵌套和就近閉合)
  • PHP樹的代碼,可以嵌套任意層
  • PHP嵌套輸出緩沖代碼實例
  • PHP函數(shù)用法詳解【初始化、嵌套、內(nèi)置函數(shù)等】
  • PHP優(yōu)化教程之解決嵌套問題

標簽:常德 澳門 佛山 深圳 工商登記 宜春 延安 宿遷

巨人網(wǎng)絡(luò)通訊聲明:本文標題《php無限級評論嵌套實現(xiàn)代碼》,本文關(guān)鍵詞  php,無限,級,評論,嵌套,實現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php無限級評論嵌套實現(xiàn)代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于php無限級評論嵌套實現(xiàn)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 永年县| 景东| 兰西县| 静宁县| 临沭县| 柳河县| 长泰县| 临沭县| 饶河县| 石柱| 基隆市| 安仁县| 安宁市| 岫岩| 图木舒克市| 吉林省| 彩票| 光泽县| 满洲里市| 连江县| 富锦市| 兴安县| 临海市| 深州市| 禹州市| 永昌县| 五台县| 花垣县| 潞城市| 铁力市| 普陀区| 泊头市| 仁怀市| 吉木萨尔县| 阿鲁科尔沁旗| 临沂市| 韶山市| 安多县| 丘北县| 安塞县| 宜州市|