Page 1 of 1

Load image's from Folder Automatically

Posted: Mon Jan 09, 2012 10:40 am
by Goofan
Hey,

My problem goes as following:
I am trying to load image's from a folder and have them display as you press next.

This is what I have come accross:

Code: Select all

function getPhoto2(){
 $imagesDir = 'Bilder/Img/';
 $images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

 return json_encode($images);
}
If I Echo this it will display this:
[text]
["Bilder\/Img\/b1.jpg","Bilder\/Img\/b10.jpg","Bilder\/Img\/b11.jpg","Bilder\/Img\/b12.jpg","Bilder\/Img\/b13.jpg","Bilder\/Img\/b14.jpg","Bilder\/Img\/b15.jpg","Bilder\/Img\/b16.jpg","Bilder\/Img\/b17.jpg","Bilder\/Img\/b2.jpg","Bilder\/Img\/b3.jpg","Bilder\/Img\/b4.jpg","Bilder\/Img\/b5.jpg","Bilder\/Img\/b6.jpg","Bilder\/Img\/b7.jpg","Bilder\/Img\/b8.jpg","Bilder\/Img\/b9.jpg"]
[/text]
The array is displayed like this:
[text]
$array[0] -> "
$array[1] -> B
$array[2] -> i
$array[3] -> l
and so on
[/text]
Meaning I can't do this:

Code: Select all

//$Fclass is pre-defined
$Image2 = $Fclass->getPhoto2();
echo "<img src='$Image2[1]' width='100px' height='100px'></a>";
Might have written wrong on the echo statement (if I did please correct me)

What have I done wrong? What can I change it into to make it work?

/Regards,
Thomas

Re: Load image's from Folder Automatically

Posted: Mon Jan 09, 2012 12:21 pm
by twinedev
You say you echoed it, where did you echo it? Looks like you echoed out what the function returned. If that is where you wanted to use the data, then you need to change the functions to just return $images; (without passing through json_encode()).

You give an example of how the "array is displayed" where are you doing that at?

In your code where you have writen how you want to use it, you sat that the $Fclass is predefined, so I take it that you mean you cannot alter it. As is, that function will only return a string, If you are stuck with that, you will just have to manipulate it, probably with json_decode

-Greg

Re: Load image's from Folder Automatically

Posted: Mon Jan 09, 2012 12:44 pm
by Goofan
Ok so I got the index.php where I echo the stuffs out.
I got the function in the Foto_Class.php

I need to pass it from the function (class) to the index file and echo it out. Am I doing anything wrong?

When I said pre-defined I simply meant that it was connected to the class file. (correctly)

The array is echoed at the index file aswell

Was this what you were asking for?

Need more information?

P.S.
If I remove the json_encode(), and then echo it out it only echoes "Array". Why is that?

Re: Load image's from Folder Automatically

Posted: Tue Jan 10, 2012 2:37 pm
by Goofan
I got it working :)