Chris James Champeau
Chris James Champeau

Reputation: 994

while loop not ending as expected

I might be crazy here, but I am pretty confused as to why my while loop seems to never end, it causes the page to continuously load and then chokes the server for several min afterwords.

Basically I have table of photo albums

+----+--------+--------+
| id | title  | parent |
+----+--------+--------+
| 1  | album1 | 0      |
+----+--------+--------+
| 2  | album2 | 0      |
+----+--------+--------+
| 3  | album3 | 2      |
+----+--------+--------+
| 4  | album4 | 3      |
+----+--------+--------+
| 5  | album5 | 4      |
+----+--------+--------+
| 6  | album6 | 1      |
+----+--------+--------+

So basically what we have here is a hierarchy of albums that is like this

- album1
  ^- album6
- album2
  ^- album3
     ^- album4
        ^- album5

I have another table of the photos themselves

+----+--------+-------+--------+
| id | title  | album | master |
+----+--------+-------+--------+
| 1  | photo1 | 1     | 1      |
+----+--------+-------+--------+
| 2  | photo2 | 2     | 2      |
+----+--------+-------+--------+
| 3  | photo3 | 6     | 1      |
+----+--------+-------+--------+
| 4  | photo4 | 4     | 2      |
+----+--------+-------+--------+
| 5  | photo5 | 5     | 2      |
+----+--------+-------+--------+
| 6  | photo6 | 3     | 2      |
+----+--------+-------+--------+

So you can see I have photos and they belong to each album, but I also want to be able to see which master album they belong to, or lowest possibly album.

I do not want to add a col to the photo albums table that says which album is its master album. If I must I can but for now I would like to try and do it without doing so.

To do this I figured I would do this with a while loop.

So when I add an album via the form I get the current album id via a hidden input field and tie it to the $parent

$parent = $_POST['album'];

and since you cannot add photos in the root directory the album id will always be greater than 1, so even if we are adding this to say album 1, right now $parent would be equal to 1

So I introduce the while tag

while ($parent>0) {

Then I get the album data for the $parent album from the database, yes I have the class already setup and working, I do not believe this is the issue as this works on its own without the while loop.

    $item = $mysql->get_data("photos_albums", $parent);

Then I check to see if the parent of the $parent album is greater than 0, thus not being a root album

    if ($item['parent']>0) {

if it is then we change the $parent to the previous albums $parent and run it again until we get an album whos parent is 0

        $parent = item['parent'];

if it is not greater than 0, then we know it is 0 and that the $parent album is the lowest possible album and the master album is this one

    } else {
        $master = $item['id'];
    }
}

All of this combines to be this code, which for me does not work, simply causes the page to continuously load.

$parent = $_POST['album'];
$master = 0;

while ($parent>0)
    $item = $mysql->get_data("photos_albums", $parent);
    if ($item['parent']>0) {
        $parent = $item['parent'];
    } else {
        $master = $item['id'];
    }
}

Am I missing something?

Upvotes: 0

Views: 88

Answers (2)

TechnicalChaos
TechnicalChaos

Reputation: 452

when you find parent isn't greater than 0 - You aren't setting the while loops conditional - $parent.

That's the thing you're missing, you're only setting $parent if it ISN'T 0. Make sure you update your conditional on both clauses.

Upvotes: 1

Nathan van der Werf
Nathan van der Werf

Reputation: 332

Parent is only reset when parent is higher than 0

if ($item['parent']>0) {
    $parent = $item['parent'];
} else {
    $master = $item['id'];
    $parent = 0;
}

should fix

Upvotes: 3

Related Questions