jacko310592 Posted November 8, 2009 Share Posted November 8, 2009 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 Link to comment https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/ Share on other sites More sharing options...
GingerRobot Posted November 8, 2009 Share Posted November 8, 2009 This is a simpler version of the procedure outlined in this FAQ: http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Basically, use a counter in a loop and add the tags where necessary. Link to comment https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/#findComment-953613 Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 <?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 https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/#findComment-953616 Share on other sites More sharing options...
jacko310592 Posted November 8, 2009 Author Share Posted November 8, 2009 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 https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/#findComment-953624 Share on other sites More sharing options...
jacko310592 Posted November 8, 2009 Author Share Posted November 8, 2009 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 Link to comment https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/#findComment-953626 Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 <?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 https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/#findComment-953628 Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 <?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 https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/#findComment-953633 Share on other sites More sharing options...
jacko310592 Posted November 8, 2009 Author Share Posted November 8, 2009 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 https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/#findComment-953656 Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 if ($i == count($files) - 2) echo '</div>'; Link to comment https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/#findComment-953664 Share on other sites More sharing options...
jacko310592 Posted November 8, 2009 Author Share Posted November 8, 2009 thank you so much it worked perfectly Link to comment https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/#findComment-953673 Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 NP Link to comment https://forums.phpfreaks.com/topic/180750-php-glob-help-glob-and-place-into-divs/#findComment-953678 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.