Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
4  
Those are equivalent arrays (save for that you're wrapping your arrays in more arrays, why?). The ['..', '..', ..] notation is just the compact literal notation. –  deceze Feb 10 at 13:51
    
just do it the normal way, variableslide[0] or in the chrome console you can type variableslide[0]; it should return the team_a/blank.jpg or the first element of the array –  Oli Soproni B. Feb 10 at 13:55

1 Answer 1

up vote 0 down vote accepted

if you wont it to look like this

variableslide[0]=['team_a/blank.jpg']
variableslide[1]=['team_a/ford.jpg']

in the php file use

<script type="text/javascript">

<?php
foreach($team_a as $i=>$v){ echo 'variableslide['.$i.']="'.$v.'";'."\n";}
?>

insetad of

<script type="text/javascript">

var variableslide = <?php echo json_encode($team_a, JSON_UNESCAPED_SLASHES)
?>

* ***Just notice that is the exact same thing !*

share|improve this answer
    
Hi Federico, thanks for the response, I tried your suggestion but then realised that you can't actually run php code within javascript, it just prints the line as text ... –  PossiblyInfected Feb 11 at 9:01
    
I also got close with the following code: –  PossiblyInfected Feb 11 at 9:02
    
// $arraycount=count($team_a); // $counter="0"; // echo " var variableslide=new Array(".$arraycount.");"; // while ($counter < $arraycount) { echo " variableslide[".$counter."]= ['".$team_a[$counter]."']\n"; $counter++; –  PossiblyInfected Feb 11 at 9:03
    
But, again, the echo just prints out the resulting javascript code on the page without making it actual javascript code ... Any ideas? –  PossiblyInfected Feb 11 at 9:04
    
Sorry I think I made an error the PHP bracket is "<?php" and not "<php?". I corrected it try it again.... –  Federico Feb 11 at 9:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.