fishbowl Posted August 11, 2006 Share Posted August 11, 2006 I need a script that will pull 20 random images from a pool of 2000, and will keep those same random 20 images for a certain time period. For example, every day a new set of 20 random images would show up.I've found a lot of random image scripts, but with all of them, the image changes every time the page is refreshed. I don't want the images to change that often, but they do need to change periodically, without my having to go into the code and change the images manually.I'm thinking about trying to modify an existing php random image script to do the above. However, I don't really know anything about PHP, and I have no idea where to begin. Quote Link to comment Share on other sites More sharing options...
corbin Posted August 11, 2006 Share Posted August 11, 2006 Dang thats a tough one.... Lemme thing for a few minutes lol Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 11, 2006 Share Posted August 11, 2006 this is fairly easy to do. if you have a database, use that - otherwise, use a simple flat file (although a db would be easier):1. check the db/flat file storage for the set of images to use. a) if the stored info's date/time is *insert interval here* older than the current, proceed to 2. b) if the stored info is NOT *interval* older than current, use the stored image info and you're done.2. use PHP to get the random images (perhaps use a pre-existing script).3. capture the images you are using (perhaps plug them into an array when you display them).3. store the contents of this array either in a db, or in a flat file, along with the current date/time.hope this makes sense. Quote Link to comment Share on other sites More sharing options...
corbin Posted August 12, 2006 Share Posted August 12, 2006 Hmmm I attached some files i made for displaying 20 random images from a folder... It stores the names of the 20 images to use and recreates that file every 24 hours... Just look through the files and you should be able to figure out whats going on...lol its not very good... I wrote it half asleep :DAlso if you have a DB i suggest using it instead of flat files...[attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
fishbowl Posted August 12, 2006 Author Share Posted August 12, 2006 Thanks for the help so far..corbin, I tried the attached files, but they don't really work right. The images change with every refresh, and the number of images changes as well. Also, there's always at least one broken image, where no image name is pulled (shows up in images.txt as a string of numbers).I really don't know anything about PHP, so I have no idea what's going on in those files. I've been trying to read through online PHP manuals to figure it out, but.. more help would be appreciated. :-\ Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 12, 2006 Share Posted August 12, 2006 Damn i just wrote the whole script here and accidently pressed F5, ill just make the mscript and tell you Quote Link to comment Share on other sites More sharing options...
corbin Posted August 12, 2006 Share Posted August 12, 2006 Hehe I was kinda tired when I wrote that earlier... I just went back through it and its kinda... ummmm not good lol... I guess RockingGroudon said hes gonna make a script so ill just let them do that :D Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 12, 2006 Share Posted August 12, 2006 I made it, the only problem is that this dosent check if the image is already in the 20and also your images shud have names like image2000.gifEDIT: Let me just go through it if i can set it to check if the images are repeating[attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2006 Share Posted August 12, 2006 not that i want to beat rockinggroudon to it, i just wanted to give you something that was hopefully easy to follow and understand. i should note that storing it in a file will require PHP 4.2.0 or greater, since it uses var_export().i should also note that you should REALLY look into using a database to do this. let us know if that's possible, as it's both more efficient and easier to write/understand. that being said, here's the function itself:[code]<?phpfunction get_images_list($list_filename, $path_to_images, $number_of_images){ // define an array of image extensions $image_extensions = array('gif', 'jpg', 'jpeg', 'png', 'bmp'); // initialise the return variable $image_list = array(); // check if the list file exists if (file_exists($list_filename)) { // include it so that it defines an array for us (called $image_list by default) include($list_filename); // check if the list is less than 24 hours old if ($image_list['timestamp'] >= (time() - 24*3600)) { // if it is less than 24 hours old, just drop the timestamp and return the list unset($image_list['timestamp']); return $image_list; } else { // if it's stale, then undo the definition the include has made unset($image_list); } } // by now it will have booted out of the function if the file's image list was useable // therefore if we're still here, we need to create a new file list (either for the first time, or to replace the stale one) // open the images directory $handle = @opendir($path_to_images); // boot out with a NULL return if the directory couldn't be opened if ($handle === FALSE) return NULL; // otherwise, go through the directory while ($filename = readdir($handle)) { // grab a lowercase version of its extension $extension = strtolower(substr(strrchr($filename, '.'), 1)); // if its extension is an image type, then log it if (in_array($extension, $image_extensions)) $image_list[] = $filename; // boot out if we've reached the limit of images if (count($image_list) == $number_of_images) break; } // close the directory closedir($handle); // add a timestamp to the array $image_list = array_merge($image_list, array('timestamp' => time())); // turn the list content into something useable $content = '<?php $image_list = '.var_export($image_list, TRUE).'; ?>'; // write the list to a file $newfile = fopen($list_filename, 'w'); fwrite($newfile, $content); fclose($newfile); // kill the timestamp again unset($image_list['timestamp']); // return NULL if it found no images, or the image list if it did return (empty($image_list)) ? NULL : $image_list;}?>[/code]in order to grab the current file list (the function will either grab the currently stored version, or create a new one and attempt to store it anew), use the following code:[code]$image_list = get_images_list('image_list.php', 'images/', 20);[/code]replacing the variables with the actual values you want to use, of course. keep in mind the list filename needs to be a .php file. i assume you know how to go through an array using a foreach(). if not, it's quite simple:[code]<?phpforeach ($image_list AS $filename){ echo '<img src="images/'.$filename.'" />';}?>[/code]however, if the function didn't work for some reason, the $image_list variable will be NULL (not an array). in order to accommodate this, you'll want to do something along the lines of:[code]<?phpif ($image_list === NULL || empty($image_list)){ echo 'No images could be grabbed.';}else{ // foreach stuff}?>[/code]furthermore, if you want to randomize the order only (but not the overall list of images), you can use:[code]shuffle($image_list);[/code]somewhere between when you grab it, and when you use the foreach() to echo the images.my aim with this is to get you to learn something new. hopefully that's what's happened. let me know if anything is foggy or if it doesn't work (i haven't tested it).[b]EDIT: my method does not require that the images be any particular name, since renaming 2000+ images would be a pain.[/b] Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 12, 2006 Share Posted August 12, 2006 It dosent matter me if you beat me, but the reason I dont use COOKIES, SESSIONS or SQL is because I make scripts so everyone can use it, and with the lowest resources.I know you came out aa li'l better than me but whatever, atleast I made it. ^_^ Quote Link to comment Share on other sites More sharing options...
corbin Posted August 12, 2006 Share Posted August 12, 2006 hehe both of yall versions was better than mine... Oh well.. Didnt put much thought or effort into mine :D Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2006 Share Posted August 12, 2006 rockinggroudon, i wasn't trying to "beat" you. i was simply pointing out that the image names didnt have to be imagenumber.whatever, in case he thought it applied to my solution as well. as for resources, cookies sessions and databases generally use fewer resources than a filesystem approach; that's irrelevant anyhow, since i'm using files.i'm not trying to show you up, i just figured i'd see if i could teach the user something new. Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 12, 2006 Share Posted August 12, 2006 I know that. You see what I meant by resources was that you need MySQL to have Databases, or neother SQL. You need to have COOKIES enabled to have cookies, and the webmaster needs to allow SESSIONS to use sessions.btw the reason i got hyper was that mostly I dont create scripts for ppl cause i have HW to, u see i am too younger to you ppl.I am in VIIth gradeThe first time I created a script for sum1 and there was sum1 to give him a better 1.You can urself know how angry i cud hav got Quote Link to comment Share on other sites More sharing options...
fishbowl Posted August 12, 2006 Author Share Posted August 12, 2006 Thanks to everyone for their responses.. this is by far the most helpful forum I've ever been on.akitchin, I tried your script, and it seems to be working. I didn't want to wait 24 hours to test it, so I took this line:[code] // check if the list is less than 24 hours old if ($image_list['timestamp'] >= (time() - 24*3600))[/code]and tried editing it so that it would change the images, say, every minute. I wasn't really sure how to format that (did several PHP.net/Google searches, came up empty handed), so I tried changing the "24*3600" to things like, "1*60", "60", "0", "1*1", "1". Waited about 2 minutes after each change to refresh, and the same random images came up each time. So, I'm not sure if that's a problem with the script, or if I formatted the time wrong.I do have a mysql database, as well as PHP 4.4. However, I literally know nothing about mysql coding.. I can install pre-written scripts, but I don't know how to write my own or edit any existing ones. Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 12, 2006 Share Posted August 12, 2006 Use mine then Quote Link to comment Share on other sites More sharing options...
fishbowl Posted August 12, 2006 Author Share Posted August 12, 2006 RockingGourdon, your code has a similar timestamp line in it, and again I can't figure out how to modify it so that I don't have to wait 24 hours to test it. I tried what I did with akitchin's code, but with yours, and got the same results. So I assume that I'm doing something wrong. My only problem with your code is that I don't want to go through and rename all 2000 images to image1.gif, image2.gif, etc. Thanks for the help, though. Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 12, 2006 Share Posted August 12, 2006 You see the timestamp is in seconds and i minus it to check if it works there so minus in seconds and for the renaming thing, you have to go through some pain sometimes, ya i know you are not suppose to go through the pain but yet just telling you if you cant get akitchins script to work.Ill see wat you do though to check if you do something wrong.Well do this to check akitchins code // check if the list is less than 24 hours old if ($image_list['timestamp'] >= (time() - 24*3600))changed to // check if the list is less than 24 hours old if ($image_list['timestamp'] >= (time() - 10))Now it should change every 10 secs Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2006 Share Posted August 12, 2006 as rocking said, it's in seconds. there are 3600 seconds in an hour, and 24 hours in a day, so i subtract 24 * 3600 seconds from the current time to get the time 24 hours ago.as for using MySQL, perhaps we'll leave that to another topic if you want to learn how to use it some day. if this works for now, might as well use it. Quote Link to comment Share on other sites More sharing options...
fishbowl Posted August 12, 2006 Author Share Posted August 12, 2006 I see. Well, I edited the number of seconds in both codes, and neither changed the set of images when I refreshed after 10 seconds.I also tried manually removing the image lists, to see if it would create a new set of random images, but the same random images are pulled each time. With both codes. :-[ Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 13, 2006 Share Posted August 13, 2006 haha whoops. i totally forgot about the random part. change the while() loop to this:[code]<?php // otherwise, go through the directory while ($filename = readdir($handle)) { // grab a lowercase version of its extension $extension = strtolower(substr(strrchr($filename, '.'), 1)); // if its extension is an image type, then log it if (in_array($extension, $image_extensions)) $image_list[] = $filename; }?>[/code]and add this between the closedir() command and the array_merge():[code]<?php$chunks = array_chunk($image_list, $number_of_images);$image_list = $chunks[0];?>[/code](minus the php tags, since those are already in your file, they're here for syntax highlighting). Quote Link to comment Share on other sites More sharing options...
fishbowl Posted August 13, 2006 Author Share Posted August 13, 2006 Okay, it's still not changing the images. The new while() loop you provided didn't look different from the original while() loop, other than the "// boot out if we've reached the limit of images" section of code being removed. I also inserted the new 2 lines. Well, just for reference, this is what it looks like so far. It's still showing the same 20 images.[code]<?phpfunction get_images_list($list_filename, $path_to_images, $number_of_images){ // define an array of image extensions $image_extensions = array('gif'); // initialise the return variable $image_list = array(); // check if the list file exists if (file_exists($list_filename)) { // include it so that it defines an array for us (called $image_list by default) include($list_filename); // check if the list is less than 24 hours old if ($image_list['timestamp'] >= (time() - 10)) { // if it is less than 24 hours old, just drop the timestamp and return the list unset($image_list['timestamp']); return $image_list; } else { // if it's stale, then undo the definition the include has made unset($image_list); } } // by now it will have booted out of the function if the file's image list was useable // therefore if we're still here, we need to create a new file list (either for the first time, or to replace the stale one) // open the images directory $handle = @opendir($path_to_images); // boot out with a NULL return if the directory couldn't be opened if ($handle === FALSE) return NULL; // otherwise, go through the directory while ($filename = readdir($handle)) { // grab a lowercase version of its extension $extension = strtolower(substr(strrchr($filename, '.'), 1)); // if its extension is an image type, then log it if (in_array($extension, $image_extensions)) $image_list[] = $filename; } // close the directory closedir($handle);$chunks = array_chunk($image_list, $number_of_images);$image_list = $chunks[0]; // add a timestamp to the array $image_list = array_merge($image_list, array('timestamp' => time())); // turn the list content into something useable $content = '<?php $image_list = '.var_export($image_list, TRUE).'; ?>'; // write the list to a file $newfile = fopen($list_filename, 'w'); fwrite($newfile, $content); fclose($newfile); // kill the timestamp again unset($image_list['timestamp']); // return NULL if it found no images, or the image list if it did return (empty($image_list)) ? NULL : $image_list;}$image_list = get_images_list('image_list.php', 'images/', 20);if ($image_list === NULL || empty($image_list)){ echo 'No images could be grabbed.';}else{foreach ($image_list AS $filename){ echo '<img src="images/'.$filename.'" />';}}?>[/code] Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 13, 2006 Share Posted August 13, 2006 again, i'm a dummy. add this line before the chunks line:[code]shuffle($image_list);[/code]hopefully that patches it up. Quote Link to comment Share on other sites More sharing options...
Chetan Posted August 13, 2006 Share Posted August 13, 2006 Thats because you might have your cache send back the same stuff.Its better that you refresh twice or put cache control in your headers Quote Link to comment Share on other sites More sharing options...
fishbowl Posted August 13, 2006 Author Share Posted August 13, 2006 [quote author=akitchin link=topic=103935.msg414818#msg414818 date=1155438675]hopefully that patches it up.[/quote]It worked!! This is awesome. Thanks so much for all the help. :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.