我正在構建一個從訂閱表中獲取用戶的函數。這些用戶然後用於獲取他們的個人文章,並以有序的方式顯示他們。從多個用戶獲取行並按日期排序顯示它們
我已經設法做到了一半。它將獲取用戶,然後輸出用戶文章。但是一次只需要一個用戶。我正在尋找一種方法來選擇我的訂閱表中的所有用戶並將它們全部提取出來,以便我可以同時顯示它們,但是按日期排序。
的表格:
**Subscribe :**
Lastviewed (datetime)
Uniqueuser (userid)
Subscription (subscribed users id)
**Article:**
date_posted (datetime)
uniqueuser (users id)
這是我得到:
$sesname = $_SESSION['full_name'];
$sesid = $_SESSION['user_id'];
connectDB();
$sql ="select * from subscribe where uniqueuser = '$sesid'";
$rsd = mysql_query($sql);
while ($row = mysql_fetch_array($rsd))
{
$subscription = $row['subscription'];
$lastviewed = $row['lastviewed'];
$substart = $row['substart'];
$sql3 = "select * from article
where uniqueuser = '$subscription'
and date_posted > '$lastviewed'
order by date_posted
desc limit 0, 7";
$rsd3 = mysql_query($sql3);
$newarticles = mysql_num_rows($rsd3);
while ($row3 = mysql_fetch_assoc($rsd3)) {
$id =$row3['id'];
$title = $row3['title'];
$uid = $row3['uniqueuser'];
$sql2 = "SELECT * FROM users WHERE id = '$uid'";
$result2 = mysql_query($sql2);
while ($row2 = mysql_fetch_assoc($result2)) {
$name = $row2['user_name'];
echo "<b><a>{$name}</a></b> wrote an <a>article</a><br>";
}
}
} //END SUBSCRIBE
此輸出:
User A wrote an article (1th july)
User A wrote an article (2th july)
User A wrote an article (3th july)
User A wrote an article (4th july)
User B wrote an article (1th july)
User B wrote an article (2th july)
User B wrote an article (3th july)
User B wrote an article (4th july)
即使用戶B的文章已經在用戶A的帖子之間發佈,它也會爲一個用戶接連排序。
通緝輸出:
User A wrote an article (1th july)
User B wrote an article (1th july)
User A wrote an article (2th july)
User B wrote an article (2th july)
User A wrote an article (3th july)
User B wrote an article (3th july)
User A wrote an article (4th july)
User B wrote an article (4th july)
讓我怎麼幫它,它有文化按日期排序,不管是什麼用戶寫的? 我想我的問題是因爲訂閱查詢的while循環,但我不知道任何其他方式來做到這一點。任何幫助是極大的讚賞! =)
請將您的代碼格式 – helle 2012-07-14 11:43:57
這樣更好嗎? :) – Havihavi 2012-07-14 12:09:19