[英]Fetching rows from MySQL table and displaying them as JGROWL notifications
[英]fetching rows from multiple users and displaying them ordered by date
我正在構建一個將從訂閱表中獲取用戶的函數。 然后,這些用戶將被用來獲取他們的個人文章並以有序的方式展示它們。
我設法做到了一半。 它將獲取用戶,然后輸出用戶文章。 但是一次只需要一個用戶。 我正在尋找一種選擇我的訂閱表中的所有用戶並提取所有用戶的方法,因此我可以同時顯示它們,但按日期排序。
表格:
**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循環,但我不知道執行此操作的任何其他方法。 任何幫助是極大的贊賞! =)
或多或少地簡單地加入所涉及的3個表並按a.date_posted排序
$sql2 = "select a.* , u.user_name, a.id as article_id, u.id as user_id
from subscribe s
join article a on s.subscription = a.uniqueuser AND s.lastviewed < a.date_posted
join user u on a.uniqueuser = u.id
where s.uniqueuser = '$sesid'
order by a.date_posted
desc limit 0, 7";
$result2 = mysql_query($sql2);
while ($row2 = mysql_fetch_assoc($result2)) {
$name = $row2['user_name'];
$date = $row2['date_posted'];
// fetch the columns having the same name using aliases
$a_id = $row2['article_id'];
$u_id = $row2['user_id'];
echo "<b><a>{$name}</a></b> wrote an <a>article</a>({$date})<br>";
}
編輯
修復條件s.lastviewed <a.date_posted
關於加入
兩個或多個表之間的聯接是具有有效/真條件(ON和WHERE)的表中所有元素的所有組合的集合
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.