0

After pulling my hair out for several hours I'm close but still can't get it.

I have a PHP script that uses a glob and a foreach to compile a nested array:

$folderDir = '*/';
$folders = glob($folderDir, GLOB_ONLYDIR);
$imagesDir = 'images/';
$images = Array();

foreach($folders as $folder){
  $images[] = glob($folder. $imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
}
echo json_encode($images);

I want to get the first image from each of the glob results' sub-folders and send them to jquery (and subsequently an shtml file):

$(window).load(function() 
{
    $.getJSON('populate.php', function(data) 
    {
        $.each(data, function() 
        {
            alert(data);
        });
    });
});

For a second I had it, but as soon as I changed the Jquery to using each it would alert the array 4 times (which may have something to do with the number of images in each folder?).

I've tried about 1,000 different variations of my variables, used 1 foreach, then 2 then 1.

I'm very new to PHP and need some second opinions.

Thank you to all who can give me some guidance!!

  • There is two possible sources of the error here. Either the PHP is not returning what you expect and describe in the first place or the jQuery is not rendering it properly. So make sure your PHP is returning the right things before debugging your jQuery. – Gordon Feb 05 '14 at 06:52
  • For a second I was getting the right array returned to me in PHP, but it was wasn't coming through right in the JS. I figured it was because there was something fundamentally wrong with my PHP and its logic. – user2912472 Feb 05 '14 at 07:00
  • Yes, you wrote that already. But tt doesn't help diagnose the problem. Like I said, either the PHP code returns the right thing, then debug the JS. Or the PHP code doesn't return the right thing, then debug that. To paraphrase: if you want to display [1,2,3,4] in JS but what you put into the JS is [4,3,2,1] then there is no point fixing the JS. So verify which part of your code is broken. I mean, really verify. Not just guess or wonder. Write a Unit Test or step through the PHP code or simply var_dump $image or inspect the JSON. – Gordon Feb 05 '14 at 07:06
  • @Gordon- When I do a `var_dump` I think my array looks as it should. I think my problem may be I just don't know how to get those first children out of there properly into an array with the same format. And after a day of research cannot figure out a good solution. – user2912472 Feb 05 '14 at 08:15
  • Theoretically, if it were possible I would call the array like `$images [i][0]`, so just keeping the parent variable and grabbing the first child of each. Sorry if I'm repeating myself, I just want to be absolutely clear. – user2912472 Feb 05 '14 at 08:23

2 Answers2

0

You have some logic mistakes:

$folderDir = '*/';
$folders = glob($folderDir, GLOB_ONLYDIR);
$imagesDir = 'images/';

$images = Array();

// you redefine glob data from parent to images
// and after that redefine images to image 
// it's unnecessary
foreach ($folders as $folder)
{
    $images[] = glob($folder . $imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
}

echo json_encode($images);

And jQuery. For debugging I recomed to use console.log command

$(window).load(function() 
{
    $.getJSON('populate.php', function(data) 
    {
        // Debuging using console.log
        console.log(data);

        // Or if you not using console.log
        $.each(data, function(key, val)
        {
             alert(key, val);
        })
    });
});
Community
  • 1
  • 1
Dmitriy.Net
  • 1,476
  • 13
  • 24
0

Well this seems to do the trick. I'm fairly certain it's not perfect syntactically but it outputs the right results. The JSON reads it now and the array is correct:

$folderDir = '*/';
$folders = glob($folderDir, GLOB_ONLYDIR);
$imagesDir = 'images/';

$image = array();
$finalArray = array();

foreach($folders as $folder){
  $images = glob($folder. $imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
  $image = array($images);
  $firstBorn = $image [0][0];
  $finalArray[] = $firstBorn;
}

echo json_encode($finalArray);

?>

This gives me all 1.jpg (or the first that appears in the array) in any */images/ folders.

Thanks to all who had input.