Question

I scan a directory with PHP which includes only folders:

$folders = scandir('gallery');

Now i want to check if a string in javascript a folder in this directory.

if(theString == allTheFolders){
     alert('yay');
}

Now $folders is an array with strings in it. To get all the strings i use a foreach loop and ignore the '.' & '..' directory's. But how can i get all these folders in the if loop? Hope you understand my question!

Was it helpful?

Solution

Echo out your array as JSON, right into your JavaScript.

echo 'var folders = ', json_encode($folders);

Then you can loop through or do whatever you need directly in JavaScript.


Edit: Now that you have posted your actual question... Do this in your JavaScript:

var wantedFolder = 'something';
var wantedFolderFound = false;
for (folderIndex in folders) {
    if (folders[folderIndex] === wantedFolder) {
        wantedFolderFound = true;
    }
}
if (wantedFolderFound) {
    alert('Folder found!');
} else {
    alert('Folder not found.');
}

As an alternative, I would probably use Array.indexOf(). It isn't available in all browsers, but that problem is easily remedied. See the documentation: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top