I have a MySQL table of posts that looks like this:
| id | post | parent |
+----+--------------------------------------+--------+
| 1 | Hello world | 0 |
| 2 | response to 1 | 1 |
| 3 | response to 1 | 1 |
| 4 | new opening post | 0 |
If a post is an opening post, the parent is set to 0. If it's a reply to an opening post, its parent is a value of an ID. I'm trying to fetch the table, print each opening post, then print responses underneath them indented. This is what I have so far, but it doesn't work:
while ($row = mysqli_fetch_assoc($result)) {
if ($row["parent"] == 0) {
echo "" . $row["id"] . " | " . $row["post"] . "<br>"; }
while ($row2 = mysqli_fetch_assoc($result)) {
if ($row["id"] == $row2["parent"]) {
echo ">" . $row2["id"] . " | " . $row2["post"] . "<br>"; }}}
I've been looking around on the internet for a solution for a while now to no avail; I've tried putting the mysql result into an array, but that didn't work either. I would really appreciate if someone could point me in the right direction. Thanks in advance!
This isn't a duplicate of How to go through mysql result twice? as I want keep going through the table instead of resetting after every OP.