Showing two images per line, max of 4

53 views Asked by At
$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.

1

There are 1 answers

5
mrjamesmyers On BEST ANSWER

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.

<?php
$dir = dir("/adunits/");
$i = 0;
while ($file = $dir->read()) {
    if($file != "." && $file != "..") {
        if (!is_dir($curpath.$file)) {
           $files[$i] = $file;
           $i++;
        } // not a directory
    } // not . or ..
} // read through dir

$dir->close();
shuffle($files);

$maxImages = 4;
$file_count = count($files);
if($file_count < 4) {
    $maxImages = $file_count;
}

?>
<h4 class="widgettitle">Sponsors</h4>
    <ul class="adunit_list">
    <?php
        for ($i = 0; $i < $maxImages; $i++) {
            $target = "http://" . str_replace(".jpg", "", $files[$i]);
            if (file_exists("/adunits/$files[$i]")) { ?>
                <li class="adunit_position"><a href="<?php echo $target ?>" target="_new"><img class="adunit" src="http://temp.com/adunits/<?php echo $files[$i]?>" alt="<?php echo $files[$i]?>" /></a></li>;
                <?php
            }
        } ?>
    </ul>

<style>
.adunit_list li {
    float: left;
}
.adunit_list li:nth-of-type(2n+1) {
    clear:  both;
}