new to JS and PHP and modifying a slideshow script to dynamically display contents of three different directories. Pretty much keeping things simple to start off with.
One problem I have is that the script I am modifying used the following static code to fill the javascript array:
variableslide[0]=['team_a/blank.jpg']
variableslide[1]=['team_a/ford.jpg']
variableslide[2]=['team_a/futuristic.jpg']
variableslide[3]=['team_a/lambo.jpg']
My PHP code is working so far to enumerate the directories dynamically and then pass the result on to Javascript:
<?php
// Header("content-type: application/x-javascript");
$team_a = array();
$team_b = array();
$team_c = array();
foreach (new DirectoryIterator('team_a') as $fileInfo) {
if($fileInfo->isDot() || !$fileInfo->isFile()) continue;
$team_a[] = "team_a".'/'.$fileInfo->getFilename();
}
foreach (new DirectoryIterator('team_b/') as $fileInfo) {
if($fileInfo->isDot() || !$fileInfo->isFile()) continue;
$team_b[] = $fileInfo->getFilename();
}
foreach (new DirectoryIterator('team_c') as $fileInfo) {
if($fileInfo->isDot() || !$fileInfo->isFile()) continue;
$team_c[] = $fileInfo->getFilename();
}
?>
<script type="text/javascript">
var variableslide = <?php echo json_encode($team_a, JSON_UNESCAPED_SLASHES)
?>
When I "inspect element" in Chrome whilst running the full code, I can see that the file names are being passed to Javascript, but, missing the 0,1,2,3,4 etc. I only end up with the file path. Here's the result:
var variableslide = ["team_a/blank.jpg","team_a/ford.jpg","team_a/futuristic.jpg","team_a/lambo.jpg"];
What I've been Googling around is how to get var variableslide array to read as such:
variableslide[0]=['team_a/blank.jpg']
variableslide[1]=['team_a/ford.jpg']
To pull the array index number and the filename from PHP and populate the variableslide array in JS so it reads the same as above. When I print_r from the PHP script, it shows the index number and text correctly. Can anybody please help me figure this one out! Thanks!
['..', '..', ..]
notation is just the compact literal notation. – deceze Feb 10 at 13:51