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

  • Home
  • SEARCH
  • 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 6887419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:52:58+00:00 2026-05-27T05:52:58+00:00

I am in the process of making a quick PHP based forum, and each

  • 0

I am in the process of making a quick PHP based forum, and each post in a forum will appear under its “parent” post, but slightly more indented.

To get all the posts in that order, I have the following function:

    private function getData($pid, $offset)
    {
            $sql = 'SELECT id, subject, date 
                    FROM post 
                    WHERE forum_id = ? AND parent_id = ?';
            $sth = $this->db->prepare($sql);
            $sth->bind_param("ii", $this->id, $pid);
            $sth->bind_result($id, $subject, $date);
            $sth->execute();

            $data = array();

            while ( $sth->fetch() )
            {
                    $row['id'] = $id;
                    $row['subject'] = $subject;
                    $row['date'] = $date;
                    $row['offset'] = $offset;

                    //Add this 'parent' post to the data array
                    $data[] = $row;

                    //Before moving on to next post, get all its children
                    $data[] = $this->getData($id, $offset+1);
            }

            $sth->close();

            return $data;
    }

This isn’t working because I am executing another query before closing and fetching all the data from my current statement handler.

Is there a way to maybe separate the queries so they don’t conflict with each other? Or any other way to by-pass this? Or will I simply have to restructure how I get my data?

  • 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-05-27T05:52:58+00:00Added an answer on May 27, 2026 at 5:52 am

    Fetch all the rows into an array, then loop over them.

    $rows = array();
    while ( $sth->fetch() ) {
        $row['id'] = $id;
        $row['subject'] = $subject;
        $row['date'] = $date;
        $row['offset'] = $offset;
        // the cool way is $rows[] = compact('id', 'subject', 'date', 'offset');
        $rows[] = $row;
    }
    $sth->close();
    
    foreach ($rows as $row) {
                    //Add this 'parent' post to the data array
                    $data[] = $row;
    
                    //Before moving on to next post, get all its children
                    $data[] = $this->getData($id, $row['offset'] + 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am in process of making my bootstrap.php file more organized, but after I
I'm in the process of making my PHP site Unicode-aware. I'm wondering if anyone
I'm in the process of making a Python-based personal assistant/question answerer, which, in my
I'm in the process of making an application, but I've encountered an problem which
I am in the process of making a PHP web application. I have a
In the process of making an ebook, users have to edit its content, edit
I'm in the process of making a browser based multiplayer turnbased strategy game similiar
I'm in the process of making an Android 3 app that has a tab-based
In the process of making an old webpage standard compliant, the validator came across
I am in the process of making a website that involves a shopping cart.

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.