Forum Moderators: coopster
Right now I've got this simple little gizmo on my page:
<?php
$img = date("z");
echo "<IMG SRC=\"images/".$img.".jpg\">";
?> I've got a bunch of images in the "images" directory that are just numbered, 85.jpg, 86.jpg, and so on, so the above script displays a specific image every day. I've been trying to find a simple script that will let me randomize the displayed image, but only every day, and not every time the page is viewed. Is there any relatively uncomplicated way to do this? I'm still a newbie and very indimidated by long, involved scripts. So far all the fiddling around I've done with rand() results in stuff that would load a different image for every pageview, which I definitely don't want. My existing little script works for all intents and purposes, I'd just like to randomize it if possible.
Or, if you've only got 31 images, then you could go by the number of the day of the month.
The problem is this: If you randomize for one pic a day, then basically you're just taking a picture and assigning it to a particular day. It's not really all that random -- it just changes every day.
When you say "long, involved scripts", how many lines are you talking about? Here are the steps I'd take if doing the script:
0. See if today has an image.
- 0.A) If so, show it.
- 0.B) If not, do 1-4:
1. open a connection with the image bin
2. read all the titles of the images.
3. randomly choose one.
4. assign that to today (by putting it into a MySQL database, or writing to a writable file)
There'll be a chance that some images get shown multiple times, while others remain obscure.
Your option B sounds like what I'm looking for. How would I go about implementing that, so that a randomly-selected image is shown every day? (I'm a little confused by the "problem" you mention; maybe I don't understand why it's a problem?)
I don't understand why it's a problem
here is what you could do
say we have 100 images each named, as you already have it, just with a number
we have a static link to an image called today.jpg in the root of the site
we select the file by cron nightly, the cron could be something like
we would need 2 dirs for this
one called active for images to be used
one called used for images that have been used already
1. see if there are any images in the active directory
2. if there aren't any move all of the images from used to active
3. load the names (numbers) of the images into an array
4. randomly select one number from the array
5. copy that image to the site root and name it today.jpg, overwriting the previous day's image
6. move the image to the used directory
that would keep you from using an image twice until every image is used. That would also allow you to add images anytime with out having to change the script.
You had me until you got to "cron," unfortunately. I know absolutely nothing about cron jobs (I see where it is in my cPanel, but have no idea what to do with it). Can you elaborate, or would it be simpler to use a MySQL database in some way, as saoi_jp hinted at?
[fixed]
[2]<?php
function rndImage(){
$folder = "images"; /// Change this to your img folder
$imgFiles = Array();
$dirHandler = opendir($folder) or die("cannot opendir");
while($myImages = readdir($dirHandler)){
if($myImages == "." ¦¦ $myImages == ".."){continue;}
array_push($imgFiles,$myImages);
}
$imgNo = rand(0, count($imgFiles));
$imgName = $folder."/".$imgFiles[$imgNo];
echo $imgName;
}
?>
[/2]
[/fixed]
[fixed][2]<IMG SRC="<?php rndImage()?>">[/2][/fixed]