假设您的网络应用程序中有40个人,而您只是其中的20个人。每个用户都发布了照片和状态更新。你如何根据时间一次性获得时间线上20个人的帖子?什么代码会有帮助? 我做了什么= 我使用while循环获取了我的朋友列表,并使用while循环再次获取更新。
$get_users = $con->query("SELECT * FROM friends WHERE person1=$myid ");
while($rows=$get_users->fetch_assoc()){
$friendsid = $rows['person2'];
//this fetches 20 of my friends
//now what i did was fetching their updates using the WHILE loop again
$get_updates = $con->query("SELECT * FROM updates WHERE userid=$friendsid ");
while($rows=$get_updates->fetch_assoc()){
$updates_status = $rows['text'];
$updates_photo = $rows['photo'];
echo " $update_status <br> $updates_photo <br> ";
}
}
但问题是,我总是首先看到同一个朋友的更新。即使他的更新是2周龄,它会在第一次显示,而第19位朋友在一分钟前更新了他的状态,这是更新的第二个。 如何根据我的朋友发布的时间获取更新?
答案 0 :(得分:0)
你的代码几乎就在那里。您只需要检索最新的20个帖子,就需要按POST_TIME
列对数据进行排序。像这样:
$get_users = $con->query("SELECT * FROM friends WHERE person1=$myid ");
while($rows=$get_users->fetch_assoc()){
$friendsid = $rows['person2'];
//this fetches 20 of my friends
//now what i did was fetching their updates using the WHILE loop again
$get_updates = $con->query("SELECT * FROM updates WHERE userid=$friendsid ORDER BY POST_TIME");
while($rows=$get_updates->fetch_assoc()){
$updates_status = $rows['text'];
$updates_photo = $rows['photo'];
echo " $update_status <br> $updates_photo <br> ";
}
}
使用正确的列名更改POST_TIME
。
答案 1 :(得分:0)
请使用以下代码:
$get_users = $con->query("SELECT * FROM friends WHERE person1=$myid ");
$friends = '';
while($rows=$get_users->fetch_assoc())
{
$friends .= $rows['person2'].',';
}
$get_updates = $con->query("SELECT * FROM updates WHERE userid IN ".substr($friends, 0, -1)." ORDER BY date"); //probably you have 'date' field
while($rows=$get_updates->fetch_assoc())
{
$updates_status = $rows['text'];
$updates_photo = $rows['photo'];
echo " $update_status <br> $updates_photo <br> ";
}