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?