sách gpt4 ai đã đi

Cách chỉ hiển thị bài viết từ danh sách bạn bè. (Chính xác)

In lại 作者:行者123 更新时间:2023-11-28 23:47:05 29 4
mua khóa gpt4 Nike

我的 friend 数组只返回一个数字而不是所有数字。

($myfriends = 3)

应该是……

($myfriends = 3 5 7 8 9 12).

如果我让它进入 while 循环……整个过程都有效,但不是像我想要的那样按日期顺序显示帖子。示例:

(post_id 3 oct30 "post goes here")
(post_id 5 oct29 "post goes here")
(post_id 7 oct28 "post goes here")

相反,它显示来自 1 个人 id 按日期排序的所有帖子。然后是那个人的所有帖子。它最终会显示下一个人和他们的所有帖子。:

(post_id 3 oct30 "post goes here")
(post_id 3 oct29 "post goes here")
(post_id 3 oct28 "post goes here")

(post_id 5 oct30 "post goes here")
(post_id 5 oct29 "post goes here")
(post_id 5 oct28 "post goes here")

等等...任何新的帖子组与用户旧帖子而不是进入提要的顶部。

我希望这是有道理的......对于糟糕的解释和草率的代码感到抱歉


下方的 TABLES 提要和 friend 两者都有自动递增主'id'作为第一个字段......然后

FEED TABLE
feed (post_id, posted_by, content, date_posted, date_str, pic) VALUES ('$post_id', '$posted_by', '$content', now(), '$date', '$profile_pic')

FRIENDS TABLE
friends (user_id, friend_id) VALUES ('$myId', '$friendId')


$addsql="SELECT * FROM friends WHERE user_id = '$session_id' ORDER by id DESC";
$addquery=mysqli_query($conn, $addsql);

while ($rowa = mysqli_fetch_array($addquery)) {
$myFriends = $rowa['friend_id'];
?>

$feedsql = "SELECT * FROM feed WHERE post_id = '$myFriends' ORDER by date_posted DESC";
$result = mysqli_query($conn, $feedsql);

while($row = mysqli_fetch_array($result)) {
$posted_by = $row['posted_by'];
$content = $row['content'];
$time = $row['date_posted'];
$date = $row['date_str'];
$pic = $row['pic'];
$post_id = $row['post_id'];
$upperUser = ucfirst($username);
?>

$imgsql = "SELECT * FROM users WHERE username = '$posted_by' ";
$q = mysqli_query($conn, $imgsql);
while($rows = mysqli_fetch_array($q)) {
$image = $rows['image'];
$mem_id = $rows['id'];
if ($posted_by == $upperUser) {
echo " MEMBER Pic";
} else if($posted_by !== $upperUser) {
echo " Profile Pic";
}
}
?>





}}
?>

1 Câu trả lời

问题本身是由于在while循环中覆盖了$myFriends变量引起的:

while ($rowa = mysqli_fetch_array($addquery)) {
$myFriends = $rowa['friend_id'];
?>

您可以将 $myFriends 定义为一个数组并将 friend_ids 添加到数组中,但是您不应该这样做,因为它效率低下。

您可以在 SQL 中使用 JOIN 关键字连接多个表。在这种特殊情况下,您将需要一个 INNER JOIN(有多种类型的联接,但您的任务是了解它们)。

$feedsql = "SELECT * FROM feed INNER JOIN friends ON feed.posted_by=friends.friend_id WHERE friends.user_id = '$session_id' ORDER by date_posted DESC";

由于您没有透露您的表结构,我假设 feed 表中的 posted_by 字段将确定帖子的发送者,因此我可以将此字段加入到 friend_id 字段中。如果不是这种情况,那么您需要确定可以在哪些字段上连接这 2 个表。

你甚至可以在上面2个表上加入users表来获取好友的详细信息。

关于php - 如何仅显示来自好友列表的帖子。 (正确),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33439153/

29 4 0
Bài viết được đề xuất: 有异常的 MySQL UPDATE 查询
Bài viết được đề xuất: ios - 获取 iOS 的活跃用户数
Bài viết được đề xuất: mysql - 如何在mysql中进行精确比较?
Bài viết được đề xuất: android - 我的自定义组件的代码隐藏未运行
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com