我正在构建一个从订阅表中获取用户的函数。这些用户然后用于获取他们的个人文章,并以有序的方式显示他们。从多个用户获取行并按日期排序显示它们
我已经设法做到了一半。它将获取用户,然后输出用户文章。但是一次只需要一个用户。我正在寻找一种方法来选择我的订阅表中的所有用户并将它们全部提取出来,以便我可以同时显示它们,但是按日期排序。
的表格:
**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