php無(wú)限級(jí)評(píng)論嵌套實(shí)現(xiàn)代碼
我在設(shè)計(jì)BB的過(guò)程中,也一直在思考是否可以不通過(guò)遞歸來(lái)實(shí)現(xiàn)無(wú)限級(jí)分類(lèi)的結(jié)構(gòu)展現(xiàn)和父子結(jié)構(gòu)查找,因?yàn)槿绻粚?duì)這里的算法進(jìn)行優(yōu)化后果可能是致命的!試想一下,一篇文章如果評(píng)論數(shù)為300,按正常的遞歸算法,至少就得查詢(xún)數(shù)據(jù)庫(kù)301次,而且還是在沒(méi)有任何嵌套的情況下,如果有過(guò)一兩級(jí)嵌套或者評(píng)論數(shù)過(guò)1000,那數(shù)據(jù)庫(kù)不是直接宕掉?
而實(shí)際上,PHP強(qiáng)大的數(shù)組處理能力已經(jīng)能幫助我們快速方便的解決這個(gè)問(wèn)題。下圖為一個(gè)無(wú)限級(jí)分類(lèi)的
數(shù)據(jù)庫(kù)結(jié)構(gòu):
IDparentID newsID commts
108文章ID為8的評(píng)論
21 8對(duì)ID為1的評(píng)論的回復(fù)
328對(duì)ID為2的評(píng)論的回復(fù)
要在前臺(tái)嵌套式的展現(xiàn)文章編號(hào)8的評(píng)論,其實(shí)我們只用查詢(xún)一次數(shù)據(jù)庫(kù),即“SELECT * FROM TABLE WHERE newsID=8”,而把后期的遞歸工作交給強(qiáng)大的PHP數(shù)組來(lái)完成。這里可能涉及的問(wèn)題就是數(shù)組的結(jié)構(gòu)關(guān)系的重組,即將所有停留在一級(jí)分類(lèi)上的評(píng)論全部放到自己的parentID下,形成children項(xiàng)。
下面將BBComment類(lèi)中這塊的代碼粘貼出來(lái),希望與大家分享下我的思路,也希望大家能夠提出更好更有效率的算法。
方法一
/** * 按ID條件從評(píng)論數(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); } } /** * 追加 子評(píng)論 到 主評(píng)論 中,并形成children子項(xiàng) * * @param array $commtAry 原評(píng)論數(shù)據(jù)引用 * @param int $parentId 主評(píng)論ID * @param array $childrenAry 子評(píng)論的值 */ 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); /* 開(kāi)始進(jìn)行嵌套評(píng)論結(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é)束 */
實(shí)現(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); //當(dāng)前評(píng)論的開(kāi)始代碼 if ($max_depth > $depth +1 && isset ($children_elements[$id])) { //如果沒(méi)超過(guò)最大層,并且存在子元素?cái)?shù)組 foreach ($children_elements[$id] as $child) { if (!isset ($newlevel)) { //第一次循環(huán)沒(méi)設(shè)置變量$newlevel,所以把$newlevel設(shè)為true,并且開(kāi)始子元素的開(kāi)始代碼;第二次及之后的循環(huán),已經(jīng)設(shè)置了$newlevel,就不會(huì)再添加子元素的開(kāi)始代碼。因?yàn)橥慌h(huán)時(shí)兄弟元素,所以只需要一個(gè)子元素開(kāi)始代碼,循環(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ù)去尋找下級(jí)元素 } unset ($children_elements[$id]); //用完釋放變量,以后就不會(huì)重復(fù)判斷該值了,遞歸后繼續(xù)判斷剩下的子元素 } if (isset ($newlevel) && $newlevel) { //如果前面找到了子元素,這里就要執(zhí)行子元素的結(jié)束代碼 end_lvl($output); } end_el($output); //當(dāng)前評(píng)論的結(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])) { //如果超出最大層級(jí),并且子元素存在的話,以$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后都會(huì)釋放變量,所以到最后$children_elements為空數(shù)組 return $output; } echo '<ol class="comment-list">', comments_list($comments), '</ol>';
這篇文章就介紹到這了,其實(shí)大家多參考一些開(kāi)源的cms也可以看到很多不錯(cuò)的代碼,希望大家以后多多支持腳本之家
相關(guān)文章
PHP常量使用的幾個(gè)需要注意的地方(謹(jǐn)慎使用PHP中的常量)
常量包含數(shù)字字母字符和下劃線,數(shù)字允許作為常量名。 常量名的所有字母必須大寫(xiě)。類(lèi)常量必須通過(guò) const 定義為類(lèi)的成員,強(qiáng)烈不鼓勵(lì)使用 define 定義的全局常量2014-09-09php的POSIX 函數(shù)以及進(jìn)程測(cè)試的深入分析
本篇文章是對(duì)php的POSIX函數(shù)以及進(jìn)程測(cè)試進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP實(shí)現(xiàn)適用于自定義的驗(yàn)證碼類(lèi)
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)適用于自定義的驗(yàn)證碼類(lèi),使用對(duì)象編寫(xiě)的驗(yàn)證碼類(lèi),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06將CMYK顏色值和RGB顏色相互轉(zhuǎn)換的PHP代碼
本節(jié)主要介紹了將CMYK顏色值和RGB顏色相互轉(zhuǎn)換的PHP代碼,需要的朋友可以參考下2014-07-07PHP實(shí)現(xiàn)重載的常用方法實(shí)例詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)重載的常用方法,結(jié)合實(shí)例形式對(duì)比java分析了php實(shí)現(xiàn)重載的四種常見(jiàn)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10PHP目錄與文件操作技巧總結(jié)(創(chuàng)建,刪除,遍歷,讀寫(xiě),修改等)
這篇文章主要介紹了PHP目錄與文件操作技巧,結(jié)合實(shí)例形式總結(jié)分析了php針對(duì)文件與目錄的獲取、運(yùn)算、打開(kāi)、創(chuàng)建、讀取、寫(xiě)入、修改、刪除、判斷等常見(jiàn)操作技巧,需要的朋友可以參考下2016-09-09