国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php無限級評論嵌套實現代碼

2024-05-05 00:03:15
字體:
來源:轉載
供稿:網友

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

數據庫結構:

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

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

方法一

/**  * 按ID條件從評論數組中遞歸查找  *  */ 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 原評論數據引用  * @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);   /* 開始進行嵌套評論結構重組 */  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);  /* 重組結束 */ 

實現方法二

核心代碼摘自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])) { //如果沒超過最大層,并且存在子元素數組    foreach ($children_elements[$id] as $child) {      if (!isset ($newlevel)) { //第一次循環沒設置變量$newlevel,所以把$newlevel設為true,并且開始子元素的開始代碼;第二次及之后的循環,已經設置了$newlevel,就不會再添加子元素的開始代碼。因為同一批循環時兄弟元素,所以只需要一個子元素開始代碼,循環內容為并列關系。        $newlevel = true;        start_lvl($output);      }      display_element_template($child, $children_elements, $max_depth, $depth +1, $output); //$child作為參數,繼續去尋找下級元素    }    unset ($children_elements[$id]); //用完釋放變量,以后就不會重復判斷該值了,遞歸后繼續判斷剩下的子元素  }  if (isset ($newlevel) && $newlevel) { //如果前面找到了子元素,這里就要執行子元素的結束代碼    end_lvl($output);  }  end_el($output); //當前評論的結束代碼}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為參數繼續往下找    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為空數組  return $output;}echo '<ol class="comment-list">', comments_list($comments), '</ol>';

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


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 上林县| 佛山市| 班戈县| 分宜县| 新和县| 谷城县| 吴川市| 蒙山县| 长武县| 鄯善县| 巧家县| 株洲县| 泰和县| 郧西县| 缙云县| 鄂托克前旗| 康定县| 项城市| 嵩明县| 仪征市| 黄平县| 张掖市| 长海县| 广昌县| 宁津县| 黑水县| 永昌县| 曲靖市| 茶陵县| 喀喇沁旗| 兰西县| 保德县| 旅游| 察哈| 花莲市| 普兰店市| 美姑县| 望都县| 扶绥县| 喀什市| 屯门区|