Increment Only the div class inside loop, inside another loop

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?


$ext =' 1.jpg';
         if ($handle = opendir('images/gallery_indiv')) {
           while (false !== ($file = readdir($handle)))
              {
              
                  if ($file != "." && $file != "..")
              {
                $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>';
                  $i++;
                  }
                  
                  }
               }
          closedir($handle);
          }
echo $thelist;

If I set it up like this my server runs into an infinate loop till I run out of allocated memory.


$ext =' 1.jpg';
         if ($handle = opendir('images/gallery_indiv')) {
           while (false !== ($file = readdir($handle)))
              {
              
                  if ($file != "." && $file != "..")
              {
                $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>';
                  
                  }
                  $i++;
                  }
               }
          closedir($handle);
          }
echo $thelist;

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)


$ext=' 1.jpg';

$i=0;

foreach (glob('images/gallery_indiv/*',GLOB_ONLYDIR) as $dirName) {
	if (($dirName!='.') && ($dirName!='..') {
		$thelist.='
			<div',(
				(($i++)%2==0) ? ' class="odd"' : ''
			),'>
				<a href="gallerytest.php?dir='.$dirName.'">
					<img
						src="images/gallery_indiv/'.$dirName.'/'.$dirName.$ext.'"
						width="300"
						alt=""
					/>
				</a>
				<div class="name">'.$dirName.'</div>
			</div>';
	}
}
	
echo $thelist; 

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.

Oh, and if you REALLY want col1 and col2, then do this:


$i=0;
foreach (glob('images/gallery_indiv/*',GLOB_ONLYDIR) as $dirName) {
	if (($dirName!='.') && ($dirName!='..') {
		$thelist.='
			<div class="col',$i++,'">
				<a href="gallerytest.php?dir='.$dirName.'">
					<img
						src="images/gallery_indiv/'.$dirName.'/'.$dirName.$ext.'"
						width="300"
						alt=""
					/>
				</a>
				<div class="name">'.$file.'</div>
			</div>';
		$i=$i%2;
	}
}

Nice thing about using modulo on the counter is that you can change how many columns just by changing the number after the %.