Menu
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.
[i]Originally posted by rhsunderground [/i]
[B]thanks for the input. i'm working it out [U]with ben[/U] on MSN. [/B][/QUOTE]
[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]
[i]Originally posted by NogDog [/i]
[B][code=php]
[/B][/QUOTE]
$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]
[i]Originally posted by Ben R. [/i]
[B]But, it's a random picture, it just changes only once a day. [/B][/QUOTE]
[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]
[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]
[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]
[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]
0.1.9 — BETA 5.9