$dir = dir("/adunits/");
$file_count = 0;
while ($file = $dir->read()) {
if($file != "." && $file != "..") {
if (!is_dir($curpath.$file)) {
$files[$file_count] = $file;
$file_count = $file_count + 1;
} // not a directory
} // not . or ..
} // read through dir
$dir->close();
shuffle($files);
echo "<h4 class=\"widgettitle\">Sponsors</h4>";
for ($i = 0; $i <= $file_count-1; $i++) {
$target = "http://" . str_replace(".jpg", "", $files[$i]);
if (file_exists("/adunits/$files[$i]")) {
echo "<center><div class=\"adunit_position\"><a href=\"$target\" target=\"_new\"><img class=\"adunit\" src=\"http://temp.com/adunits/$files[$i]\" alt=\"$files[$i]\" /></a></div></center>";
}
}
I need to modify this to show two images per line and a max of 4 images. Right now the directory has only 4 images but in the near future it will have many more... probably like 100.
I did not write this code and I am trying to learn and grow in my php knowledge.
Thank you in advance for your help.
With regards to Max Images you can create a variable with a default value of 4 e.g
$maxImages = 4;
and then change this value based on the file count and then loop over $maxImages.I would consider changing the HTML markup a bit to use an Unordered List
<ul><li></li></ul>
and using CSS to limit the amount of images on a line to 2. In below example (untested) I have used nth-of-type and CSS floats however there may be more modern/better ways of achieving desired result. https://css-tricks.com/almanac/selectors/n/nth-of-type/ I have also replaced echo ""; with breaking in and out of PHP which allows for better syntax highlighting. Also means you don't need to use escape characters to use double quotes. I would say take a look at http://php.net/manual/en/language.types.string.php to learn a bit more about usage of single quote and double quotes.