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
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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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>';
   }
?>

Link to comment
Share on other sites

<?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>';
   }
?>

 

Link to comment
Share on other sites

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 (:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.