This script runs twice making the image appear 2x’s then going on to the next image. I would like to change the div class=col$i every picture till it gets to the condition. Any help?
The reason you get an infinite loop in the second example is because you never increment the counter:
$i=1;
while($i<=2)
{
$thelist .= '<div class="col'.$i.'"><a href="gallerytest.php?dir='.$file.'"><img src="images/gallery_indiv/'.$file.'/'.$file.$ext.'" width="300"></a><div class="name">'.$file.'</div></div>';
// you should add one to $i here, as in the first example
// but you don't, so $i will always be 1
}
I’m not sure what to suggest to fix it, because I’m not sure what exactly you want. Could you maybe post the final HTML that you want to build?
IF I’m reading what you want to do correctly, you want it to alternate between 1 and 2 regardless of how many there are – so set I outside the loop, not inside it… that way you don’t reset the number. I’d probably use modulus on that to get the remainder so it’s a simple addition.
Your code seems needlessly complex though – if I’m reading what you’re trying to do properly, it appears you want to pull all the directories (and only the directories!) from that path – so I’d use GLOB instead of all that needlessly complex directory handling since it has a directory only filter. Some formatting on your output wouldn’t hurt either… also, if you only have two columns, why not only output a class on the odd columns, since that should be all you need for styling them. (the whole “not every ejaculation deserves a name” routine)
That would output class=“odd” on the odd numbered ones and no class on the evens – which if you have a parent container with a good name on it will save you a good deal on the markup – If you REALLY want them numbered, just swap the inline conditional for:
<div class=“col’,(++$i)%2,'”>
which will make them alternate between col0 and col1… though this kind of looks like a list of thumbnails, so wouldn’t LI be a more appropriate choice than div? Also wondering if that nested div around the text is necessary, but I’d have to see what it looks like on that. (I hate excess markup for nothing).
Wondering why you’re wasting memory on building that list instead of just echoing that out… especially since if you’ve got compression or buffering on it’s making a copy anyways.