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 9103161
In Process

The Archive Base Latest Questions

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

Hello guys i have these tables; USERS( user_id ,fullname ,username etc.) POSTS ( post_id,

  • 0

Hello guys i have these tables;

USERS( user_id ,fullname ,username etc.)

POSTS ( post_id, user_id, post, orig_post_id, replyto date)

USER_PROFILE (id, user_id, profile_image_path etc)

Examples

USERS (1 ,John Doe ,johndoe etc.),( 2 ,Stack Flow ,stackflow etc.)

POSTS (2, 1, My naame is John doe and i approve this message, 0, 0,sometimestamp),
      (3, 12, My naame is Stack Flow and i approve this message, 0, 2,sometimestamp)

USER_PROFILE (1, 1, ppdjodjf.jpg etc),(2, 2, grsdjodjf.jpg etc)

Basically, i want the query to output this if the replyto field is 0

array('post_id' => 2, 
'user_id' => 1, 
'post' => the post, 
'orig_post_id,' => 0
'replyto,' => 0
username => johndoe,
fullname => John Doe,
profile_image_path => etc)

And when it’s not zero

array('post_id' => 3, 
'user_id' => 2, 
'post' => Another post, 
'orig_post_id,' => 0
'replyto,' => 2
username => stackflow,
fullname => Stack Flow,
profile_image_path => etc,
'replies' => array(all the replies)
  • 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-17T01:34:06+00:00Added an answer on June 17, 2026 at 1:34 am

    After spending the whole day on this, My code ended up looking like this.

      public function get_connected_post()
        {
            $user_id = $this->session->userdata('user_id');
    
            $sql = "SELECT  p.*,up.fullname,u.username,upi.file_path_thumb,IF(hi5c.hi5_count is NULL,'0',hi5c.hi5_count) AS count_hi5,
                            IF(brn.branch_count is NULL,'0',brn.branch_count) AS count_branch, IF(rply.reply_count is NULL,'0',rply.reply_count) AS count_reply,
                            IF((SELECT COUNT(*) FROM post_highfives ph WHERE ph.user_id = $user_id AND ph.post_id = p.post_id),'1','0') AS count_is_hi5ed,
                            IF((SELECT COUNT(*) FROM posts pt WHERE pt.user_id = $user_id AND pt.is_branch_of_id = p.post_id),'1','0') AS count_is_branched
                    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   user_profile up
                    ON     up.user_id = p.user_id
                    JOIN user_profile_images upi
                    ON     upi.image_id = up.profile_image_id
                    JOIN users u
                    ON     u.user_id = p.user_id
                    LEFT OUTER JOIN (SELECT ph.post_id, count(*) AS hi5_count
                                     FROM post_highfives ph
                                     GROUP BY ph.post_id) hi5c
                    ON p.post_id = hi5c.post_id
                    LEFT OUTER JOIN (SELECT pst.post_id,pst.is_branch_of_id, count(*) AS branch_count
                                     FROM posts pst
                                     GROUP BY pst.is_branch_of_id) brn
                    ON p.post_id = brn.is_branch_of_id
                    LEFT OUTER JOIN (SELECT pst.post_id,pst.reply_to, count(*) AS reply_count
                                     FROM posts pst
                                     GROUP BY pst.reply_to) rply
                    ON p.post_id = rply.reply_to
                    ORDER BY p.post_date DESC";
    
    
            $query = $this->db->query($sql);
            if ($query) {
                $result = array();
                    foreach($query->result_array() as $r){
                        $branch_id = $r['is_branch_of_id'];
                        if($branch_id != 0){
                            $branch_array = $this->branch_query($this->postid_return_user_id($branch_id),$branch_id);
                        }else{
                            $branch_array = array();
                        }
                        $result[] = array_merge((array)$branch_array, (array)$r);
                    }
                return $result;
            }
             else {
                return false;
            }
    
    
        }
    

    I know this looks different from the question i asked but i was trying to simplfy what i was actually doing.I didn’t think asking a question stating this problem would get me anywhere, although simplyfying the question still didn’t get me anywhere as no one understood me haha. For my question anyway, the relevant part is the last bit of the code where i put a condition ssaying if branch_id is not zero get some array data and if it is return an empty array.The I merged the array together with then query result.
    Now i have to think of how i can simplyfy this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello guys sorry if the question looks stupid to you. i have 3 tables
Hello guys i have dataform in silverlight 4 project item source is: ItemsSource={Binding Data,
Hello guys, I have been trying to implement the DSUM function but failed to
Hello Guys! I started learning python GUI development. Let's say I have this script:
Hello guys i have been using JavaFx scene builder to build a gui that
Hello guys i have a pretty standard div with css and i would like
Hello guys i have a logical problem...i had to do a kinda fo labyrinth
Hello guys i have set background image on my input text and text area
Hello guys I have a confusing question! If I'm using WCF to create a
Hello guys i have an website online wich access the database. The database access

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.