admin管理员组文章数量:1327849
I have a jquery function which relies on an array of images to create a fadeOut/In effect.
The line of code looks like this:
var images=new Array('/images/myImage1.jpg','/images/myImage2.jpg','/images/myImage3.jpg');
Currently I manually create this array but I would like to create it using php to grab the images in a directory on my server. I have found the following code which does this but I need to format so it looks like the javascript above.
<?php
$dir = 'chamberImages/portfolio';
$files2 = scandir($dir, 1);
print_r($files2);
?>
I have a jquery function which relies on an array of images to create a fadeOut/In effect.
The line of code looks like this:
var images=new Array('/images/myImage1.jpg','/images/myImage2.jpg','/images/myImage3.jpg');
Currently I manually create this array but I would like to create it using php to grab the images in a directory on my server. I have found the following code which does this but I need to format so it looks like the javascript above.
<?php
$dir = 'chamberImages/portfolio';
$files2 = scandir($dir, 1);
print_r($files2);
?>
Share
Improve this question
asked May 16, 2011 at 12:35
TomTom
13k50 gold badges153 silver badges247 bronze badges
0
3 Answers
Reset to default 6You could just JSON encode the array that you have in $files2
, removing the .
and ..
entries with array_slice
:
var images = <?php echo json_encode(array_slice($files2, 2)); ?>;
You can use json:
json_encode($files2);
The result is a json formatted string that can be used as javascript code to create the array.
Use ajax. If you can't or do not want to do it, here is an inline PHP solution:
var images=new Array('<?php echo implode("', '", $files2) ?>');
本文标签: Create javascript array using php to get images from directoryStack Overflow
版权声明:本文标题:Create javascript array using php to get images from directory - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742215295a2434457.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论