Load image's from Folder Automatically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Load image's from Folder Automatically

Post 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
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Load image's from Folder Automatically

Post 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
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Load image's from Folder Automatically

Post 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?
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Load image's from Folder Automatically

Post by Goofan »

I got it working :)
Post Reply