/    Sign up×
Community /Pin to ProfileBookmark

is it possible to have PHP run a random picture each day? i’ve never used PHP and just got a new host that will run it, so i thought i’d see if it will do this for me.

to post a comment
PHP

20 Comments(s)

Copy linkTweet thisAlerts:
@Ben_RogersSep 29.2004 — Well, that depends. Are we talking day of the week, day of the month, or day of the year? I'll help you more once I know what you want to do, but once you start using PHP you'll find all of these easy. For more info on getting the date, see the date function in the PHP manual. <http://us2.php.net/date>
Copy linkTweet thisAlerts:
@Bootsman123Sep 29.2004 — If the images are named as 1.jpg, 2.jpg, 3.jpg etc. Then you could simply use the [URL=http://nl2.php.net/rand]rand()[/URL] function. If you've got a map filled with images of which one should be choosen then it's a little bit more difficult. But you should really give some more information on how you want to do it.
Copy linkTweet thisAlerts:
@rhsundergroundauthorSep 29.2004 — thanks for the input. i'm working it out with ben on MSN.
Copy linkTweet thisAlerts:
@MstrBobSep 29.2004 — [i]Originally posted by rhsunderground [/i]

[B]thanks for the input. i'm working it out [U]with ben[/U] on MSN. [/B][/QUOTE]


May God be with you, then... :p

Perhaps this simplest method would be to have an array with the image locations, and then to simply select from the array. Simple math, I guess, but whichever.
Copy linkTweet thisAlerts:
@Ben_RogersSep 29.2004 — Mm, but what he wants is for say, 'pie.jpg' to show up on the 13th, and 'chicken-attack.jpg' to show up on the 14th. So, that wouldn't really work.
Copy linkTweet thisAlerts:
@NogDogSep 29.2004 — [code=php]
$picList = array(1 => "pic_a.jpg",
"pic_b.jpg",
"pic_c.jpg",
# etc....
"pic_ee.jpg");
$todaysPic = $picList[date("d")];
echo "<img src='$todaysPic'>";
[/code]
Copy linkTweet thisAlerts:
@MstrBobSep 29.2004 — [i]Originally posted by NogDog [/i]

[B][code=php]
$picList = array(1 => "pic_a.jpg",
"pic_b.jpg",
"pic_c.jpg",
# etc....
"pic_ee.jpg");
$todaysPic = $picList[date("d")];
echo "<img src='$todaysPic'>";
[/code]
[/B][/QUOTE]


Exactly!
Copy linkTweet thisAlerts:
@Ben_RogersSep 29.2004 — But, it's a random picture, it just changes only once a day.
Copy linkTweet thisAlerts:
@Paul_JrSep 29.2004 — [i]Originally posted by Ben R. [/i]

[B]But, it's a random picture, it just changes only once a day. [/B][/QUOTE]

[font=palatino linptype]Exactly. You put all the image paths in an array, with the keys being the days on which the pictures should appear. Then you just use the method above, and voila.[/font]
Copy linkTweet thisAlerts:
@Ben_RogersSep 29.2004 — But, I think he wants something like, say, there are 200 images in a directory, and he wants one to be pulled out, and that one to be used the entire day. All right? Do you get what I'm trying to say, and why that method won't work?
Copy linkTweet thisAlerts:
@Paul_JrSep 29.2004 — [i]Originally posted by Ben R. [/i]

[B]But, I think he wants something like, say, there are 200 images in a directory, and he wants one to be pulled out, and that one to be used the entire day. All right? Do you get what I'm trying to say, and why that method won't work? [/B][/QUOTE]

[font=palatino linotype]How will it not work? Each image in the array is assigned a numerical key which is the day of the month that the image should appear on. Then you just use the method above to display the image. I don&#8217;t see how it won&#8217;t work. ?[/font]
Copy linkTweet thisAlerts:
@Ben_RogersSep 29.2004 — Because it's RANDOM. A random image is selected, and used for that day. So, maybe chicken.jpg will be used one day, and happy-duck.jpg the next. Also, for the BEST performance, you should be able to just add images to the dir, without worrying about the array.
Copy linkTweet thisAlerts:
@Paul_JrSep 29.2004 — [font=palatino linotype]Ah ha, I see now. ? Well, you would have to be able to keep track of which image is used&#8230; you couldn&#8217;t use cookies, and I don&#8217;t know if Rizzo has a database available. Maybe a text file? When the page loads, check to see if the file is empty &#8212; if it is, pick a random image and put the path in the text file. If the file isn&#8217;t empty, use the filename contained therein.[/font]
Copy linkTweet thisAlerts:
@MstrBobSep 29.2004 — Then read the files into an array. Simple enough.
Copy linkTweet thisAlerts:
@Ben_RogersSep 29.2004 — Re-read the last so many posts, Bob.

Yes, I was thinking a file with the date, and the pic being used. So that if the date was different than the current date, update the file. Once the file was current, with a pic unique to the day, and the current date, then just get that pic, and print out the path.
Copy linkTweet thisAlerts:
@JupacSep 30.2004 — [code=php]
$random = "data.txt";
$fp = file($random);
srand((double)microtime()*1000000);
$rl = $fp[array_rand($fp)];
echo "$rl";


#data.txt
<img src="1337.net" alt="1337" />
<img src="1337.net" alt="1337" />
<img src="1337.net" alt="1337" />
<img src="1337.net" alt="1337" />
[/code]


make it to 24 hours
Copy linkTweet thisAlerts:
@NogDogSep 30.2004 — Not really random, but it will do a different picture each day until each has been seen, then start over (I think, haven't tested it):
[code=php]
$picList = array("pic_a.jpg",
"pic_b.jpg",
"pic_c.jpg",
# etc....
"pic_z.jpg");
# use remainder of Julian date / number of elements in array:
$todaysPic = $picList[date("z") % count($picList)];
echo "<img src='$todaysPic' alt='Picture of the day'>";
[/code]
Copy linkTweet thisAlerts:
@rhsundergroundauthorOct 04.2004 — so how would i work in whatever the best script is for this situation (random picture every day) into this page?

(i feel so dirty. i feel like...........a noob <shudder />)

[upl-file uuid=f48df107-1ced-4509-80d3-f94d02381d96 size=2kB]template.txt[/upl-file]
Copy linkTweet thisAlerts:
@Ben_RogersOct 04.2004 — Well, I suppose if the pic of teh day was <img src='/potd.php' />, then once the image was decided, (in potd.php) then it could redirect to that image... or, if the page was .php, then the script itself could be placed there. Also, an include() could be used, where potd.php is invluded, and prints out the correct code.
Copy linkTweet thisAlerts:
@NogDogOct 04.2004 — If I were going to do it, I'd set up a specific directory that just had the applicable picture files in it. In the following example, I've assumed it is called "img/random" and is right under your root web directory (probably "public_html").

WARNING: Untested!

[code=php]
<div id="navBeta" title="rightbar">
<h2>Pic of teh Day</h2>
<?php
$imgDir = "img/random"; # change this value as applicable
if ($handle = opendir($imgDir))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$picList[] = $file;
}
}
echo sprintf("<p><img src='%s/%s' alt='Picture of the day'></p>n",
$imgDir,
$picList[date("z") % count($picList)]);
closedir($handle);
}
else
{
echo "<p>Sorry, this feature is not currently available.</p>n";
}
?>
</div>
[/code]
×

Success!

Help @rhsunderground spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...