Page 1 of 1
How do I design a random image gallery?
Posted: Sun Jan 27, 2008 5:25 am
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...
Re: How do I design a random image gallery?
Posted: Sun Jan 27, 2008 5:34 am
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.

Try something and let us know if you hit a wall.
Re: How do I design a random image gallery?
Posted: Sun Jan 27, 2008 5:49 am
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.

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.
Re: How do I design a random image gallery?
Posted: Sun Jan 27, 2008 5:54 am
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.
Re: How do I design a random image gallery?
Posted: Sun Jan 27, 2008 6:14 am
by ranman
thanks a lot that pretty much solved it!
Re: How do I design a random image gallery?
Posted: Sun Jan 27, 2008 6:42 am
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...