php無限級評論嵌套執行個體介紹

來源:互聯網
上載者:User

我在設計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);  
    /* 重組結束 */ 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.