Question

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.

Was it helpful?

Solution

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 :

$albums = array(
    array(
        'name' => "Album 1", 
        'photos' => array('photo1.jpg', 'photo2.jpg'), 
    ), 
    array(
        'name' => "Album 2", 
        'photos' => array('photo3.jpg', 'photo4.jpg'), 
    ), 
);

And, then, you just can work with the $albums array.


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_file or json_decode to read it.

But it means a bit more work -- so maybe not that necessary in your case ?

OTHER TIPS

Save the data in a separate json file, and use PHP's json_decode() function to parse it into PHP objects.

Using this approach, you've also got easy access to the data in JavaScript via AJAX, if you ever decide to move in that direction.

This way is easier to work with than storing data in your PHP file, and it also means that you can trivially update the data programmatically if the need ever arises.

In PHP you would probably define this as an array, ie.

$albums = array(
    0 => array('name' => 'Album 1', 'photos' => array('photo1.jpg', 'photo2.jpg')),
    1 => array('name' => 'Album 2', 'photos' => array('photo3.jpg', 'photo4.jpg'))
);

You can do the exact same in PHP:

$albums = array(
    array(
        'name'   => 'Album 1',
        'photos' => array(
            'photo1.jpg',
            'photo2.jpg',
        )
    ),
    array(
        'name'   => 'Album 2',
        'photos' => array(
            'photo3.jpg',
            'photo4.jpg',
        )
    )
);

But this is bad style. You might want to create a class:

class Album {

    protected $name;

    protected $files;

    public function __construct($name) {
        $this->name = $name;
    }

    public function addFile($file) {
        $this->files[] = $file;
    }

    public function getFiles() {
        return $this->files;
    }

}

$album1 = new Album('Album1');
$album1->addFile('photo1.jpg');
$album1->addFile('photo2.jpg');

$album2 = new Album('Album2');
$album2->addFile('photo3.jpg');
$album2->addFile('photo4.jpg');

$albums = array($album1, $album2);

Don't forget about serialize

serialize — Generates a storable representation of a value

serialize works on PHP objects too which JSON and XML don't

var_export and json_encode are possibilities.

file_put_contents($path_to_file, json_encode($albums));

there are number of possibilities

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