Jump to content

PHP Glob Help (glob and place into DIVs)


jacko310592

Recommended Posts

hey guys,

below is my code for using the glob function to grab all images from a folder and show the images on the output...

 

 

<?php $files = glob("{*.JPG,*.jpg}", GLOB_BRACE);

for ($i=0; $i<count($files); $i++)

{ $photoId = $files[$i];

echo '

<img src="'.$photoId.'" height="125px" />

';

}

?>

 

 

 

the only thing is, i need the code to insert the grabbed images into DIVs of four images per div

  eg...

 

 

 

<div>

1.jpg  2.jpg  3.jpg  4.jpg

</div>

<div>

5.jpg  6.jpg  7.jpg  8.jpg

</div>

etc etc

 

 

 

 

can anyone think of a way of doing this?

any help is greatly appreciated :D

Link to comment
https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/
Share on other sites

<?php
$files = glob("{*.JPG,*.jpg}", GLOB_BRACE);
   for ($i = 0; $i < count($files) - 1; $i++)
   {
      if ( (($i + 1) % 4) == 0)
         echo '<div>' . "\n\r";

         $photoId = $files[$i];
         echo "\t" . '<img src="' . $photoId . '" height="125px" />' . "\n\r";

      if ( (($i + 1) % 4) == 0 || $i == (count($files) - 1) )
         echo '</div>' . "\n\r";
   }
?>

 

That do the job?

hi Andy-H

 

thank you for replying, the code you just proved gave me this result...

 

 


	<img src="DSCF4412.jpg" height="125px" />

<div>

<img src="DSCF4413.jpg" height="125px" />

</div>

<img src="DSCF4414.jpg" height="125px" />

<img src="DSCF4436.jpg" height="125px" />

<img src="DSCF4453.jpg" height="125px" />

<div>

<img src="DSCF5162-E.jpg" height="125px" />

</div>

<img src="DSCF5164.jpg" height="125px" />

<img src="DSCF5874.jpg" height="125px" />

<img src="DSCF5889.jpg" height="125px" />

<div>

<img src="DSCF5890.jpg" height="125px" />

</div>

<img src="DSCF5892.jpg" height="125px" />

<img src="DSCF5893.jpg" height="125px" />

<img src="DSCF5896.jpg" height="125px" />

<div>

<img src="DSCF5926.jpg" height="125px" />

</div>

<img src="DSCF5950.jpg" height="125px" />

<img src="DSCF5952.jpg" height="125px" />


</div>			

 

 

any idea how to fix this?

think ive just figured it out

 

	<?php
$files = glob("{*.JPG,*.jpg}", GLOB_BRACE);
   for ($i = 0; $i < count($files) - 1; $i++)
   {
      if ( (($i - 0) % 4) == 0)
         echo '<div>' . "\n\r";

         $photoId = $files[$i];
         echo "\t" . '<img src="' . $photoId . '" height="125px" />' . "\n\r";

      if ( (($i - 3) % 4) == 0 || $i == (count($files) - 1) )
         echo '</div>' . "\n\r";
   }
?>

 

 

thanks allot once again guys, ive been looking for this solution for ages

:D

<?php
$files = glob("{*.JPG,*.jpg}", GLOB_BRACE);
echo '<div>' . "\n\r";
   for ($i = 0; $i < count($files) - 1; $i++)
   {
      if ( (($i + 1) % 4) == 0 && $i != (count($files) - 1) )
         echo '</div>' . "\n\r" . '<div>' . "\n\r";

         $photoId = $files[$i];
         echo "\t" . '<img src="' . $photoId . '" height="125px" />' . "\n\r";

      if ( $i == (count($files) - 1) )
         echo '</div>';
   }
?>

<?php
$files = glob("{*.JPG,*.jpg}", GLOB_BRACE);
echo '<div>' . "\n\r";
   for ($i = 0; $i < count($files) - 1; $i++)
   {
      if ( ($i % 4) == 0 && $i != (count($files) - 1) && $i != 0 )
         echo '</div>' . "\n\r" . '<div>' . "\n\r";

         $photoId = $files[$i];
         echo "\t" . '<img src="' . $photoId . '" height="125px" />' . "\n\r ";

      if ( $i == (count($files) - 1) )
         echo '</div>';
   }
?>

 

great code Andy (Y)

 

one little problem though, if i have a number of photos within a dir which doesnt add upto 4 pics per div, the code will not put the </div> at the end,

 

eg:

<div>1.jpg 2.jpg 3.jpg 4.jpg</div>

<div>5.jpg 6.jpg

 

any idea how to make it so the max number of images within a div is 4, but it can contain less?

 

 

 

thanks (:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.