Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:34:28+00:00 2026-06-17T04:34:28+00:00

I’m tying to create a share like feature on my site. I have these

  • 0

I’m tying to create a share like feature on my site. I have these tables

USERS

  1. user_id
  2. fullname
  3. username
    etc.

POSTS

  1. post_id
  2. user_id
  3. post
  4. orig_post_id
  5. date

USER_FOLLOWERS

  1. follow_id
  2. user_id
  3. follower_id
  4. date

I have this query to select posts from the users the current user is following.

//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;

Then the branch query

   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();
    }

1st, getting the data i need in the post_array.

if the field orig_post_id is not 0, that is the post was shared by a user from another post. i create another array called branch_array, branch array is meant to contain the original post’s users username,fullname and the orignal post itself.
This is where branch query comes in. With branch query i pass the orignal users user id and the original post id then it returns the original post users username,fullname and the post itself. I then get this as in the branch data variable and put it into the branch array.

Now i try to merge branch data to post_array and convert the merged array into an object.
MY desired ouput would look something like these;

scenario 1, when orig_post_id is not 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
) 

As you can see, the branched data has been merged.
scenario 2, when 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
) 

Right now it only get’s only one branch data and places it outside the object.
Any help would be deeply appreciated.
Sorry for the length. As you can tell from the harry potter posts, i’m literally going cray lol
Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:34:29+00:00Added an answer on June 17, 2026 at 4:34 am

    That’s just making things more complicated than it should be.
    Try this

     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);
    
                }
    

    Since post_array is multidimensional, you will need this function to convert it into an object.

     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;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.