The problem is you have no relationship enforcement, so no matter what you do you'll end up with complicated queries or magnitude increasing loops. Both of which will slow your code down.
The best thing to do would be to simply add the parent id to the child table since you have a 1 to many relationship. Fortunately that can be done easily by slicing up the existing id, so long as the format is identical for every record a regex can be used to match the x.x.x values out of the existing id. You can then either retain the child's id as x.x.x.Y, or you can use a composite key of the parent/child with x.x.x and Y.
Querying than becomes exceedingly easy:
SELECT pt.text, c.text
FROM parent_table pt
INNER JOIN child_table c ON c.parent_id = pt.id
ORDER BY pt.id, c.id ASC
Which now becomes a single loop. Formatting is then controlled simply by tracking changes in the parent. Using seeking is very slow and should be avoided in favour of ordering instead.