質問

この質問がすでに尋ねられた、または答えられたならば、私は本当に申し訳ありませんが、私が必要なものを見つけることはできません。

私はこれを建てた他のすべての部分を持っています、私の唯一の質問はインラインコメントを取り巻くことです。FacebookがXコメントをすべて表示するためのボタンでXコメントをレンダリングする場所に似たものをやりたいです。

しかし、これをすることができるわずか2つの方法は次のとおりです。

  1. 各項目をレンダリングするループで選択を実行する(私はこれがひどい決断であると私と同意するだろう誰もが)
  2. news_idが特定のサブセット内にある場合は、すべてのコメントを引いてからPHPを使用してそれらを繰り返すには、最近のXを選択して、残りを無視してください。

    これらのうちの1つは良い解決策のようではないようです。しかし、彼らは両方ともリソースの巨大な無駄を含みます。

    誰もがこれを実装するための潜在的な提案を持っていますか?

役に立ちましたか?

解決

SELECT * FROM comments_table WHERE article_id = {something} LIMIT {no_of_comments_per_page} SORT BY date DESC
.

これはコメントに対する非常に単純なまま強力なクエリです。

実際のコード

<?php
$sql = "SELECT * FROM comments_table WHERE article_id = 24 LIMIT 40 SORT BY date DESC";
$data = mysql_query($sql);
$comments = mysql_fetch_assoc($data);
foreach($comments as $comment){
  $ct++;
  echo "ID: {$ct}";
  echo "<br />";
  echo "Comment: {$comment["comment"]} by {$comment["user"]}";
  echo "Date: {$comment["date"]}";
}
?>
.

他のヒント

句に追加された制限で選択してください.DESC順で 'ID'で順序付けします。「コメントから*を選択してください3 desc」

ユーザが「より多くのコメントを読み込む」ボタンをクリックすると、「コメントから*から* SELECT * SELECT X、3 DESC」に似たクエリでAjax要求を実行します。

I would do a JOIN, and sort the data out after. Something like:

SELECT articles.id, articles.text, articles.date, 
       comments.id, comments.text, comments.date
FROM articles LEFT JOIN comments ON (comments.article_id = articles.id)
WHERE {some criteria} ORDER BY articles.date, comments.date

The performance issue of having extra comments that would be hidden is really negligible - and you don't have the added overhead of making an ajax request to load the other comments when they click that 'see all' button - you can just hide them, and then display them instantly.

If you wanted only the top 40 posts, you would have to add this condition to the where clause (in fact, you could wrap whatever filtering you're doing in as well):

WHERE articles.id IN (SELECT id FROM articles ORDER by articles.date LIMIT 40)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top