假设您的网络应用程序中有 40 人,而您只与其中 20 人成为 friend 。每个用户都发布了照片和状态更新。如何根据时间一次性获取你时间线中20个人的帖子?什么代码有帮助? 我做了什么= 我使用 while 循环获取了我的 friend 列表,并使用 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> ";
}
}
<小时/>
但问题是,我总是先看到我同一个 friend 的更新。即使他的更新是两周前的,也会在第一个更新中显示,而一分钟前更新状态的第 19 个 friend 则显示在倒数第二个更新中。 如何根据仅我的 friend 发布的时间获取更新?
最佳答案
请使用以下代码:
$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> ";
}
关于php - 如何使用 PHP MYSQL 创建一个时间线,其中包含根据时间随机选择的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46830985/