我知道這已被問了幾次,但我已經嘗試了所有在這個網站上的答案,我仍然沒有得到任何地方。使用MYSQL和PHP循環發佈帖子和相關注釋
我正在嘗試創建一個twitter類型的feed,它會循環發佈帖子,然後爲feed中的每個帖子(類似於Facebook)圈出相關注釋。我不確定是否最好的方法是兩個單獨的查詢或全部在一個查詢中,以及如何循環全部?
我的數據庫結構如下:
--TWEETS-- --COMMENTS-- --USERS--
id id id
accountno accountno accountno
userid userid email
createdat createdat name
tweet tweetid
image comment
我的PHP函數如下:
function gettweets(){
$result = mysql_query("SELECT tweets.id,tweets.tweet,tweets.createdat,tweets.userid,users.name,users.avatar,users.biog FROM tweets INNER JOIN users ON tweets.userid = users.id WHERE tweets.accountno ='123456' ORDER by tweets.ID DESC LIMIT 20");
while($row = mysql_fetch_array($result)) {
$name = $row['name'];
$biog = $row['biog'];
$avatar = $row['avatar'];
$newtweet= $row['tweet'];
$tweetid = $row['id'];
$timeago = ago($row['createdat']);
$thistweet = '';
$thistweet .= "<div class='tweet' data-tweetid='$tweetid'>";
$thistweet .= "<div class='left'>";
$thistweet .= "<img width='45' height='45' src='$avatar' alt='placeholder+image'>";
$thistweet .= "</div><!--/-->";
$thistweet .= "<div class='right'>";
$thistweet .= "<span class='name'>$name says</span>";
$thistweet .= "<p>";
$thistweet .= "$newtweet";
$thistweet .= "</p>";
$thistweet .= "<span class='replybar'> $timeago <a class='like' data-tweetid='$tweetid' href='' title=''>Like</a> | ";
$thistweet .= "<a class='favourite' data-tweetid='$tweetid' href='' title=''>Favourite</a> | ";
$thistweet .= "<a class='mainreply' href='' title=''>Reply</a></span>";
//I WANT TO LOOP OUT THE COMMENTS HERE
$thistweet .= "</div><!--/--><span class='clear'></span></div><!--End Tweet -->";
return $thistweet;
}
}
**編輯* * *
我我已經嘗試了下面的方法來創建它,我不是w成功地通過每條推文下面的相關評論來圈出'推文'。但是我的問題是,它是環出我的「鳴叫」的評論每一個數字,我對那個特定的tweet即
tweet 1
-comment 1.1
tweet 1
-comment 1.1
-comment 1.2 (Tweet 1 has 2 comments so loops out tweet 1 two times)
tweet 2
-comment 2.1
tweet 2
-comment 2.1
-comment 2.2
tweet 2
-comment 2.1
-comment 2.2
-comment 2.3 (Tweet 2 has 3 comments so loops out tweet 2 three times)
我覺得這是我的SQL查詢作爲即時通訊的問題新的使用JOIN聲明。這是我目前對我的查詢
"SELECT tweets.accountno, tweets.id,tweets.tweet,tweets.createdat,tweets.userid,users.name,users.avatar,users.biog,comments.comment FROM tweets INNER JOIN users ON tweets.userid = users.id JOIN comments ON tweets.id = comments.tweetid WHERE tweets.accountno ='123456' ORDER by tweets.ID DESC LIMIT 20"
有人能夠幫忙嗎?非常感激?
感謝您的支持。這似乎是有道理的。稍後當我回家時,我會嘗試第二種方法。我想我可能不得不重新編寫SQL查詢,因爲我需要它從三個不同的表中獲取。用戶,推文和評論。謝謝:-) – 2012-04-25 10:05:12
有什麼想法嗎? – 2012-04-25 19:46:10
你是說你有或不想讓推文多次顯示,如果它有多個評論。例如,如果推文有3條評論,你是否希望它發佈'TWEET,COMMENT1,COMMENT2,COMMENT3'或'TWEET,COMMENT1,TWEET,COMMENT2,TWEET,COMMENT3'? – jedwards 2012-04-25 19:51:00