How do I design a random image gallery?

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
ranman
Forum Newbie
Posts: 3
Joined: Sun Jan 27, 2008 5:18 am

How do I design a random image gallery?

Post by ranman »

I want to make a random image gallery to display a lot of funny pictures that I have... I'd like it to use mySQL or something to store captions and titles... and also manage sessions so that it wouldn't show a picture someone has already seen (randomly, but it could still be selected by title)... I'm very experienced in micro-controllers, asm, python, java, c, and c++ but I've never really done much with PHP... I know css and html but not very well... I've never done very many things for web programming... I'll be able to figure a lot of things out for myself but if someone could give me a hint in the right direction I'd appreciate it.

I was wondering about the design... should I store the images in a blob on the database? or on the server and just reference the locations in the table?

Should I even use sql?

How do I keep the image selection random but exclude certain database members?

Thanks alot for any help and I apologize in advance for my english and punctuation. English is like... my third langauge...
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: How do I design a random image gallery?

Post by JAM »

Good enought english to me, and welcome!

There is no need, I personally think, to store the images themselves in the database. To get you started, you could look into the glob() and shuffle() function(s), for gathering a list of images (or their names) from a dir.

Upon the user views an image, file the imagename in a 'seen' field in the database. Upon the user visits the page again, check against the 'seen' field and if shown = show another random image.

Just to get you started. :wink: Try something and let us know if you hit a wall.
ranman
Forum Newbie
Posts: 3
Joined: Sun Jan 27, 2008 5:18 am

Re: How do I design a random image gallery?

Post by ranman »

JAM wrote:Good enought english to me, and welcome!

There is no need, I personally think, to store the images themselves in the database. To get you started, you could look into the glob() and shuffle() function(s), for gathering a list of images (or their names) from a dir.

Upon the user views an image, file the imagename in a 'seen' field in the database. Upon the user visits the page again, check against the 'seen' field and if shown = show another random image.

Just to get you started. :wink: Try something and let us know if you hit a wall.
Thanks a lot for your quick reply! It was very helpful and I'm working on a psedo-code script now
I was wondering if the image is in the seen field in the database won't it be in that field for all the users not just the one person browsing at that time? The users do not logs in they will be managed with sessions... but if there are better ways please tell me.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How do I design a random image gallery?

Post by VladSun »

Generate a random number and store it in the user session.
Then use it as a seed for MySQL RAND() function. The trick is to order your query by this random. By using seed it's guranteed that every time your result is the same 'randomly ordered' result.
To show pictures that are not been already viewed use a LIMIT to make a 'pagination'. Store the 'page' number in the user session and incerement it by one when need.

So, this way, you'll need to store only the random seed and the page number in the user session. Also, queries are not complex.
There are 10 types of people in this world, those who understand binary and those who don't
ranman
Forum Newbie
Posts: 3
Joined: Sun Jan 27, 2008 5:18 am

Re: How do I design a random image gallery?

Post by ranman »

thanks a lot that pretty much solved it!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: How do I design a random image gallery?

Post by JAM »

Another approach, is to store viewed images (or id's) in a cookie, and check against that... Pseudo code, only examples;

Code: Select all

    $seen = array('foo.jpg', 'moo.jpg', 'kitten.jpg', 'mofo.jpg');
    $data = serialize($seen);
    $_COOKIE['seen'] = $data;
    $returndata = unserialize($_COOKIE['seen']);    
    if (in_array($yournewimage, $returndata)) {
        // another imagecheck...
    }
base64_decode() might also be an interesting function to use with serialize() above...
Post Reply