Familiarity: HTML/XHTML/HTML5 + CSS + PHP
Time Required: 5 Minutes or less
You can do really neat things with the raw power of Arrays. I showed you an example of parsing Multidimensional arrays in PHP in my previous post. In this post, I’m going to show you how to create a PHP gallery within 5 minutes. The basic assumption is that the reader is familiar with XHTML/CSS and general programming buzz words (e.g. variables, functions, strings, arrays, etc.)
Here’s what we’ll be creating at the end of this post:
There, a pretty simple gallery that has been sexified (if that is even a term) using Lightbox 2 and has raw pagination powers, thanks to the array_chunk() PHP function. Okay, before you get totally impatient to see how does this work, I should better start showing you “how”:
First, let’s create a basic markup structure:
Save the file as “index.php” in your root project directory, i.e., Gallery. Notice that I’ve downloaded Lightbox 2, extracted it to the project directory (Gallery, in this case) and linked the downloaded files from our main index.php file.
Now the CSS part (css/gallery.css)
Now is the time to get PHP back! Create a new file named “gallery.php” in the project root directory and type this code:
As you can see, I’ve wrapped the code inside a function for portability. The function takes one argument — $path. Save the script and return back to “index.php”. In this file, you need to do only two things to get the Gallery running:-
- Include the PHP code file, gallery.php, using include()
- Call the function — genGallery(), supplying the required parameter.
That’s it! Demo
Note that there are lots of things that can be improved even in this tiny little script. And there are way too many nice little things that could make this gallery a rockstar! But, I only had 10-15 minutes to create this script and write this post so..
If I were to spend an hour on this script, it’d had all these things:
- Check proper file extensions (pretty easy to implement)
- Add “star rating” feature so people could give your image FIVE STARS!
- Add tagging support
- Add commenting
- Pretty URLs
- CRUD, preferably using SQLite
- Authentication, using SQLite
Might sound like a lot but all the things listed above are pretty easy to implement in PHP. Ah, and suppose if Object Orientation was used at that! If you like this post, go ahead and create your versions (including added functionalities/improvements).
Imagine, Learn and Create.