PHP/Jquery - Variable that stores the number of files in a folder and then use it as a loop counter -
short version need creating variable stores number of files in folder , use loop counter.
full version: thought found answer here, isn't working me. since don't have enough reputation, can't comment on old thread here am.
i created jquery function add images portfolio page. is:
$(document).ready(function() { (i = 1; <= 9; i++) { $('.flex-container').append('<aside class="flex-item"><img src="images/portfolio/' + + '.jpg" alt="blah blah"></aside>'); } });
as can see, started 9 images. however, i'd site automatically update when additional images added 'images/portfolio' folder. (images named in numerical order such 1.jpg, 2.jpg, etc.)
based on thread linked above, created php file named numberoffiles.php
. file contains:
<?php return iterator_count(new directoryiterator('../images/portfolio')); ?>
(my root directory contains both 'images' , 'scripts' folder these files contained in.)
i edited js file this:
$(document).ready(function() { $.get('numberoffiles.php', function(data) { var count = data; }); (i = 1; <= count; i++) { $('.flex-container').append('<aside class="flex-item"><img src="images/portfolio/' + + '.jpg" alt="blah blah"></aside>'); } });
i tried both $.get
code inside , outside of $(document).ready(function)
, neither worked.
something tells me i'm missing simple here.
it's simple. ajax data not received when iterate for(). can use promises or deferred, or $.ajax function parameters.
you can try without change code:
$(document).ready(function() { $.get('numberoffiles.php', function(data) { var count = data; (i = 1; <= count; i++) { $('.flex-container').append('<aside class="flex-item"><img src="images/portfolio/' + + '.jpg" alt="blah blah"></aside>'); } }); });
good luck.
Comments
Post a Comment