以下のスレッド化されたコメントの例をオンラインで見つけましたが、著者はそれが彼にとって非常にうまく機能すると言っています。ただし、スレッド化されたコメントが適切な場所にあるため、結果の順序付けに問題があります。これは、例が私に与えるものです:
例の作成者は次のように述べています。列を編集し、次のような PHP コードを使用して書式設定します:"
サンプルデータ
ID | Comment | Path
---+------------------------------+----------
0 | Comment #1 | 00
1 | Comment #1 reply | 00_01
2 | Comment #1 reply reply | 00_01_02
3 | Comment #2 | 00
4 | Comment #3 | 00
5 | Comment #3 reply | 00_04
サンプル SQL
SELECT * FROM comments ORDER BY path
サンプル PHP
while ($result = mysql_fetch_assoc($query)) {
$nesting_depth = count(explode("_", $result['path']));
$branch = str_repeat("--", $nesting_depth);
echo $branch {$result['comment']}";
}
例の結果
Comment #1
-- Comment #1 reply
---- Comment #1 reply reply
Comment #2
Comment #3
-- Comment #3 reply
正確なデータを MySQL データベースに追加してテストしたところ、期待どおりに動作しました。ただし、テーブル内のデータの順序を変更すると、機能しません。説明します:
新しいREALISTICテストデータ
ID | Comment | Path
---+------------------------------+----------
0 | Comment #1 | 00
1 | Comment #2 | 00
2 | Comment #3 | 00
3 | Comment #3 reply | 00_04
4 | Comment #1 reply | 00_01
5 | Comment #1 reply reply | 00_01_02
行 [4] と行 [5] を見てください。これらの返信コメントは最後に追加されたコメントであり、結果の順序が大幅に変更されています。
新しいテスト結果
Comment #1
Comment #2
Comment #3
-- Comment #1 reply
---- Comment #1 reply reply
-- Comment #3 reply
これは大問題です!この男は絶対にくだらないことを言っているのですか、それとも私は何か間違ったことをしましたか? データが表示したい順序とまったく同じでない限り、機能しません。注文を修正するためにできる簡単なことはありますか?