无论用户的ID

时间:2015-06-05 00:57:34

标签: php mysqli

我正在尝试构建一个系统,用于检索登录用户碰巧的用户帖子"关注"。系统有效,但是用户帖子没有按照我想要的方式排列。也就是说,它们根据已查询的用户ID按簇/集排列。

例如,如果用户1有三个帖子,他的三个帖子将组合在一起;如果用户2有五个帖子,则他的五个帖子将被组合在一起,而不管该帖子的ID是否与其他用户的帖子相关。

我希望帖子不要聚集在一起,而是按顺序(id或日期)显示,而不管发布帖子的用户是谁。

我不确定我是否已经很好地解释了这个问题,但这是我的服务器端代码[下面]。

非常感谢任何帮助。 谢谢

PHP/MYSQL

public 
    function get_posts_of_followed() {
        include "connect.php";
        $userid = $this->getUserId();


        // Posts from users the loggedin user follows
        $postArray = array();


        // Get the user_ids of the people the loggedin user follows
        $followed = array();
            $get_followed_query = "SELECT `followed_id` FROM `user_follows` WHERE `follower_id` = '$userid'";
            $get_followed_result = mysqli_query($con, $get_followed_query);
                while($row_followed = mysqli_fetch_assoc($get_followed_result)){
                    $ids = array("followed_ids" => $row_followed['followed_id']);
                    array_push($followed, $ids);
                }

            foreach($followed as $theirid){

                // Get post_data from users the loggedin user follows
                $query_posts = "SELECT `id`,`post_title`,`user_id`, `post`, `date`, `category` 
                                 FROM `user_posts` 
                                 WHERE `user_id`= '". $theirid['followed_ids']."' ORDER BY `id` DESC";
                $post_result = mysqli_query($con, $query_posts);
                while($postData = mysqli_fetch_assoc($post_result)){

                    // Get profile_data from the users the loggedin user follows
                    $query_userData = "SELECT `id`,`firstname`, `lastname`, `profilepic` FROM `users` WHERE `id` = '". $postData['user_id'] ."'";
                    $userData_result = mysqli_query($con, $query_userData);
                       while($userData = mysqli_fetch_assoc($userData_result)){

                        $data = array("firstname" => $userData['firstname'], "lastname" => $userData['lastname'], "profilepic" => $userData['profilepic'], "post_title" =>  $postData['post_title'], "user_id" => $userid, "date" => $postData['date'], "post_id" => $postData['id'], "the_post" => $postData['post'],"category" => $postData['category']);
                        array_push($postArray, $data);
                       }   
                 }
             }
             return $postArray;

     }

}

0 个答案:

没有答案