合并来自两个查询输出的数据

时间:2013-01-08 18:13:41

标签: php mysql sql

我想在我的网站上创建一个类似共享的功能。我有这些表

USERS

  1. USER_ID
  2. 全名
  3. 用户名 等
  4. 帖子

    1. POST_ID
    2. USER_ID
    3. orig_post_id
    4. 日期
    5. USER_FOLLOWERS

      1. follow_id
      2. USER_ID
      3. follower_id
      4. 日期
      5. 我有这个查询来从当前用户关注的用户中选择帖子。

        //user_id from session data
        $user_id = $this->session->userdata('user_id');
        
        $sql = "SELECT  p.*,u.fullname,u.username
                FROM    (
                         SELECT  user_id
                         FROM    user_followers
                         WHERE   follower_id = $user_id
                         UNION ALL
                         SELECT  $user_id
                        ) uf
                JOIN    posts p
                ON      p.user_id = uf.user_id
                JOIN users u
                ON     u.user_id = p.user_id
                ORDER BY p.post_date DESC";
        
        
        $query = $this->db->query($sql);
        
         if ($query) {
        
                    foreach ($query->result() as $row) {
                        $branch_id = $row->orig_post_id;
                        $post_array[] = array(
                            'post_id' => $row->post_id,
                            'user_id' => $row->user_id,
                            'post' => $row->post,
                            'is_branch_of_id' => $branch_id,
                            'post_date' => $row->post_date,
                            'fullname' => $row->fullname,
                            'username' => $row->username
                        );
        
        
        
                     #i would explain what i'm tying to do here below
        
        
              if ($branch_id != 0) {
                    $branch_array = array();
                    #this contains the orignal posts user id
                    $user_branch_id = $this->postid_return_user_id($branch_id);
                    $branch_data = $this->branch_query($user_branch_id, $branch_id);
                    $branch_array[] = array(
                        'branch_uname' => $branch_data->username,
                        'branch_fname' => $branch_data->fullname,
                        'orig_post' => $branch_data->post
                    );
        
                    $post_obj = (object)array_merge($branch_array, $post_array);
                } else {
                    $post_obj = (object)$post_array;
                }
        
        
                    }
                    return $post_obj;
        

        然后是分支查询

           public function branch_query($orig_post_user_id, $orig_post_id)
            {
                $sql = "SELECT  users.username,users.fullname,posts.post,posts.post_id
                        FROM   users u
                        JOIN    posts p
                        ON      p.user_id = u.user_id
                        WHERE   u.user_id = $orig_post_user_id
                        AND     p.post_id = $orig_post_id";
                $q = $this->db->query($sql);
                return ($q)?$q->result():array();
            }
        

        1,在post_array中获取我需要的数据。

        如果字段orig_post_id不为0,则该帖子由用户从另一个帖子共享。我创建另一个名为branch_array的数组,分支数组意味着包含原始帖子的用户名,全名和原始帖子本身。 这是分支查询的来源。使用分支查询我传递orignal用户用户ID和原始帖子ID然后它返回原始帖子用户用户名,全名和帖子本身。然后我在分支数据变量中得到它并将其放入分支数组中。

        现在我尝试将分支数据合并到post_array并将合并后的数组转换为对象。 我想要的输出看起来像这样;

        方案1,当orig_post_id不为0时

        $post_obj = new stdClass([post_id] => 4,
                                [user_id] => 2,
                                [post] => ok ginny,
                                [orid_post_id] => 3,
                                [post_date] => some timestamp,
                                [fullname] => Harry Potter,
                                [username] => avadakedevra,
                                [branch_uname] => ginny,
                                [branch_fname] => Ginny Potter
                                [orig_post] => stop it harry
        ) 
        

        如您所见,分支数据已合并。 方案2,当orig_post_id = 0时

        $post_obj = new stdClass([post_id] => 3,
                                [user_id] => 1,
                                [post] => stop it harry,
                                [orid_post_id] => 0,
                                [post_date] => some timestamp,
                                [fullname] => Ginny Potter,
                                [username] => ginny
        ) 
        

        现在它只获得一个分支数据并将其放在对象之外。 任何帮助将深表感谢。 抱歉这个长度。正如你从哈利波特的帖子中可以看出的那样,我真的会去克雷哈尔 再次感谢。

1 个答案:

答案 0 :(得分:2)

这只会让事情变得更加复杂。 试试这个

 foreach ($query->result() as $row) {
                $branch_id = $row->is_branch_of_id;
                $user_branch_id = $this->postid_return_user_id($branch_id);
                $post_array[] = array(
                    'post_id' => $row->post_id,
                    'user_id' => $row->user_id,
                    'post' => $row->post,
                    'is_branch_of_id' => $branch_id,
                    'post_date' => $row->post_date,
                    'fullname' => $row->fullname,
                    'username' => $row->username,
                    'file_path_thumb' => $row->file_path_thumb,

                    'data' => $this->branch_query($user_branch_id, $branch_id)

                );
                    $post_obj = $this->array_to_object($post_array);

            }

由于post_array是多维的,因此您需要使用此函数将其转换为对象。

 public function array_to_object($array) {
        $obj = new stdClass;
        foreach($array as $k => $v) {
            if(is_array($v)) {
                $obj->{$k} = $this->array_to_object($v);
            } else {
                $obj->{$k} = $v;
            }
        }
        return $obj;
    }