第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 PHP 中構(gòu)建線程消息的嵌套列表時遇到問題

在 PHP 中構(gòu)建線程消息的嵌套列表時遇到問題

PHP
冉冉說 2021-09-18 16:44:47
我正在構(gòu)建一個非常簡單的線程消息系統(tǒng),主要用于我站點的用戶在我擴展站點功能時向我和彼此傳達錯誤和問題。但是,我沒有運氣創(chuàng)建每個線程的正確嵌套列表。我有兩個表:Threads 和 Posts,我在此處對其進行了簡化以進行演示。Threads 表大致如此,按最近修改的線程排序:+----+-----------------+---------------------+| id | thName          | thModified          |+----+-----------------+---------------------+| 5  | Thread Number 5 | 2019-06-29 20:54:59 |+----+-----------------+---------------------+| 4  | Thread Number 4 | 2019-06-29 20:45:22 |+----+-----------------+---------------------+| 3  | Thread Number 3 | 2019-06-29 20:44:20 |+----+-----------------+---------------------+| 2  | Thread Number 2 | 2019-06-29 20:43:00 |+----+-----------------+---------------------+| 1  | Thread Number 1 | 2019-06-29 20:39:25 |+----+-----------------+---------------------+其中每個帖子都與另一個表上的一個線程相關(guān),并且通過解析 parentID 列在此表中確定父/子關(guān)系。以“0”為父節(jié)點的帖子是根節(jié)點。我的基本攻擊計劃是這樣的:獲取所有線程,按最近排序?qū)τ诿總€線程,通過匹配 thread_ids 獲取所有帖子,按 parent_id 排序?qū)τ诿總€線程,以某種方式(遞歸?)遍歷這個帖子列表并創(chuàng)建一個 PHP 有序列表,正確縮進顯示父子之間的關(guān)系??杀氖?,這是過去 3 天讓我完全停滯不前的最后一步。從該數(shù)組中,我想生成一個類似于以下內(nèi)容的嵌套列表:Thread Number 5 - First message of thread number 5     Thread Number 5 - Second response to post 5     Thread Number 5 - First response to post 5         Thread Number 5 - First response to post 6請注意,響應按發(fā)布日期排序(最近的優(yōu)先),當然對于后續(xù)線程,我希望縮進再次回到零位置。添加以澄清意圖:在生產(chǎn)中,每個帖子都是一個鏈接,打開以顯示消息的全文。響應將是相同的“線程名稱”,并附加了用戶和日期。因此,例如,胎面可能會顯示“登錄期間發(fā)現(xiàn)錯誤”,而我的回復(第一個孩子)將顯示:“登錄期間發(fā)現(xiàn)錯誤 - Chris Conlee 19/07/01 09:10”我意識到上面的示例在沒有語境。老實說,我沒有任何運行良好的代碼可以在這里發(fā)布。有一次,我有一個遞歸例程,它只遍歷最左邊的那條腿,然后跳過第二個響應到帖子 5。在另一點上,我有一個例程,以重復和三次顯示所有節(jié)點,并且縮進從未正常工作。我深表歉意,因為它看起來應該是一個非常簡單的練習,但我只是把自己弄成一個結(jié),試圖讓我的頭腦圍繞它的遞歸性質(zhì),再加上多線程等。如果有人能給我一條生命線非常感謝。
查看完整描述

1 回答

?
HUH函數(shù)

TA貢獻1836條經(jīng)驗 獲得超4個贊

好吧,我終于放慢了速度,逐個元素地遍歷迭代并弄清楚了。


提供與給定線程相關(guān)的一系列帖子,例如:


[0] => Array

        (

            [post_id] => 5

            [thread_id] => 5

            [parent_id] => 0

            [user_id] => 9

            [post_message] => First message of thread number 5

            [post_created] => 2019-06-29 20:54:59

            [thread_title] => Thread Number 5

        )


    [1] => Array

        (

            [post_id] => 6

            [thread_id] => 5

            [parent_id] => 5

            [user_id] => 9

            [post_message] => First response to post 5

            [post_created] => 2019-06-29 21:39:00

            [thread_title] => Thread Number 5

        )


    [2] => Array

        (

            [post_id] => 7

            [thread_id] => 5

            [parent_id] => 5

            [user_id] => 9

            [post_message] => Second response to post 5

            [post_created] => 2019-06-29 21:52:00

            [thread_title] => Thread Number 5

        )


    [3] => Array

        (

            [post_id] => 8

            [thread_id] => 5

            [parent_id] => 6

            [user_id] => 0

            [post_message] => First response to post 6

            [post_created] => 2019-06-29 21:55:00

            [thread_title] => Thread Number 5

        )

進入以下方法:


public function buildForum($postsToThread, &$forum, $parent_id = 0) {

    foreach ($postsToThread as $post) {

        $time = strtotime($post['post_created']);

        $tmpCurrentAuthorName = $this->getPostAuthor($post['user_id']);

        $tmpCurrentThreadTitle = $post['thread_title'];

        $tmpCurrentPostDate = date("M d, Y g:i A", $time);

        if ($post['parent_id'] == $parent_id) {

            $forum .= "<ol><li><a href='/freetools/forumViewPost/" .$post['post_id'] . "'>" . $tmpCurrentThreadTitle .= " by " . $tmpCurrentAuthorName . "</a> on " . $tmpCurrentPostDate . "</li>";

            $parent_id = $post['post_id'];

            $this->buildForum($postsToThread, $forum, $parent_id);

            $parent_id = $post['parent_id'];

            $forum .= "</ol>";

        }

    }

}


查看完整回答
反對 回復 2021-09-18
  • 1 回答
  • 0 關(guān)注
  • 119 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號