0

我正在尝试回应用户朋友的帖子。基本上是一个类似于您登录时在 facebook 上的主页的系统。目前,在回显某人朋友的帖子时,在该用户的数据库表中,它有一个文本文件的地址。在这种情况下,我的内容是:1,3,数字是我朋友的用户 ID。然后在我想回显我朋友帖子的页面中是这样的:

 <?php 
            //echo $user_data['friend_list'];
            $file = $user_data['friend_list'];

            if($file != null)
            {
                $myFile = $file;
                $fh = fopen($myFile, 'r');
                $theData = fgets($fh);
                fclose($fh);
                //echo $theData;

                $friend = explode(',', $theData);

                //echo "This is friend array size ".count($friend)."</br>";


                for($i=0; $i < count($friend); $i++)
                {

                    if($i >= 0)
                    {
                        echo "I'm friend ".$i." and I have the User ID ".$friend[$i]."</br>";


                        $result = mysql_query("SELECT * FROM posts WHERE user_id = $friend[$i] ORDER BY timestamp");
                        while($row = mysql_fetch_array($result))
                        {
                          $user_id = $row['user_id'];
                          echo $row['content'] . " " . username_from_user_id($user_id) . " said on ".$row['timestamp'];
                          echo "<br />";
                        }

                        echo "<br/>";
                    }


                }

            }

            ?>

目前返回/回显这个:

 I'm friend 0 and I have the User ID 1
 Hey, this is the first post. Well, I hope you like this network! harrison said on 2013-03-25 22:50:52
 Hey, this is the second post ever on this website. I am testing out retrieving text from a database! harrison said on 2013-03-25 22:52:52
 I really like the post system so far! harrison said on 2013-03-25 22:57:52
 Okay, so far on user's profiles, it shows the six latest posts. harrison said on 2013-03-25 23:06:55
 Hey Chinzo! harrison said on 2013-04-13 17:11:46

 I'm friend 1 and I have the User ID 3
 Hey! matt said on 2013-03-29 00:07:05
 haha matt said on 2013-03-29 01:31:12

我怎样才能按时间戳顺序回显所有帖子?我目前正在对每个用户的帖子进行单独排序,但不是将两个用户的所有帖子1 and 3作为一个整体进行排序。我怎么能这样做?

4

1 回答 1

0

不是一次for处理一个朋友的循环,而是在一个查询中获取所有帖子:

$result = mysql_query("SELECT * FROM posts WHERE user_id IN ($theData) ORDER BY timestamp") or die ("Query error: ". mysql_error());

while ($row = mysql_fetch_assoc($result)) {
  echo $row['content'] . " " . username_from_user_id($row['user_id']) . " said on ".$row['timestamp']. "<br>";
}
于 2013-04-14T03:56:34.473 回答