donmqryh49993 2015-06-05 00:57
浏览 9

无论用户的ID如何,都可以按帖子ID排列项目

I am trying to build a system that retrieves posts from users that a logged-in user happens to "follow". The system works, however user posts do not get arranged the way I would like. That is, they are arrange in clusters/sets depending on the user id that has been queried.

For example, if user one has three posts, his three posts will be grouped together; if user two has five posts, his five posts will be grouped together, irrespective of what that post's id is relative to posts other users have made.

I would like posts not to cluster together, but to display in order (of id or date) irrespective of the user that has made the post.

I am not sure if I have explained the problem well enough, but here is my server-side code [below].

Any help is greatly appreciated. Thank you

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀