I am building a small gallery that will only have 20 or so images in it. I’d like to have this data stored somewhere (even in the PHP file itself) and so am looking at ways of encoding the data as a literal or resource file.
In JavaScript, I would use notation resembling (from memory):
var albums = [
{ name='Album 1', photos=['photo1.jpg', 'photo2.jpg'] },
{ name='Album 2', photos=['photo3.jpg', 'photo4.jpg'] }
]
This is JSON, in essence.
I suppose JavaScript is more dynamic than PHP and so this is not possible. Is there a simple alternative, perhaps using XML and binding to some classes?
I’m a complete PHP novice, so please don’t assume any knowledge in your answer.
If you just have some constant data that you want to store in a PHP file, you can use the same thing as you did in Javascript : just declare a PHP array.
Something like this should do the trick :
And, then, you just can work with the
$albumsarray.Of course, it’s not easy to update, you have to write some valid PHP code — but if you only have a couple of images, like you said, it should not be that hard to manage.
Another solution would be to store the data in an external file (in XML or JSON, for instance), and use something like
simplexml_load_fileorjson_decodeto read it.But it means a bit more work — so maybe not that necessary in your case ?