I have a PHP page that grabs images from a directory and puts the file names into an Array.
I can display the images on the PHP page using a foreach loop so I know the PHP is grabbing the file names.
I script src=getImages.php on my web page but cannot get the images in the array.
After a lot of googling I think I need JSON or JSON_encode or JSON implode. But after many iterations of trial and error I'm here asking for help.
I want the image names in the array from the getImages.php to be available to work with in the javascript.
Here's some code snippets.
getImages.php
- <?php
- $path = 'images/';
- function getImages($path) {
- $images = array();
- if ( $images_dir = @opendir($path) ) {
- while ( false !== ($img = readdir($images_dir)) ) {
- if ( preg_match("/(\.gif|\.jpg|\.png|\.jpeg)$/", $img) ) {
- $images[] = $img;
- }
- }
- closedir($images_dir);
- }
- return $images;
-
- }
- $imgList = getImages($path);
- ?>
webpage snip:
- <section class="roundabout">
- <div class="slide">
- <script src="getImages.php"></script>
- <script>
- var x= $(".slide");
- function displayImages() {
- var i = 0,
- len = imgList.length;
- for (; i < imgList.length; i++) {
- x.append("<article><a href='" + imgList [i] + "'><img class='image featured' src='" + imgList[i] + "'></a></article>");
- }
- };
- $(function() {
- displayImages();
- });
- </script>
- </div>
- </section>
Thank you in advance!
bh