PDA

View Full Version : Randomness... on PHP



Kitsune Devil
March 28th, 2004, 13:59
Hello,
Suppose I want a PHP code which allows me to show a random image (with a link that corresponds with the image) on my page. I have the images stored in a folder... now what do I do? :confused2 I'm quite new at this...
~Kitsune

keith
March 28th, 2004, 21:47
first thing's first... you'll need some type of database that stores the image names with a corresponding link.

bloodyveins
March 29th, 2004, 06:57
the solution could be either listing all files in that directory (using php functions: opendir, readdir, is_file) and then generating seed to choose random image or listing all files manually in a flat file and then choosing a random image from it.

i think, there is no need of database. this process can be done automatically.

Kitsune Devil
March 29th, 2004, 16:00
Ah, I see. Thanks!! :-)

keith
March 29th, 2004, 17:53
Originally posted by bloodyveins
the solution could be either listing all files in that directory (using php functions: opendir, readdir, is_file) and then generating seed to choose random image or listing all files manually in a flat file and then choosing a random image from it.

i think, there is no need of database. this process can be done automatically. well, that's what i was getting at. as far as i'm concerned, even a single file storing info is a type of database.

you could even store them in the actual php file, which is about as rudementary of a database as you could get:
<?php

$rand = rand(1,5);

switch ($rand) {
case 1:
echo "<a href=\"http://www.domain.com\"><img src=\"domain.jpg\"></a>";
break;
case 2:
echo "<a href=\"http://www.something.com\"><img src=\"something.jpg\"></a>";
break;
case 3:
echo "<a href=\"http://www.another.com\"><img src=\"another.jpg\"></a>";
break;
case 4:
echo "<a href=\"http://whatever.com\"><img src=\"whatever.jpg\"></a>";
break;
case 5:
echo "<a href=\"http://slappystick.com\"><img src=\"slappystick.jpg\"></a>";
break;
}

?>

bloodyveins
April 2nd, 2004, 11:34
err... i think i misdelivered my idea keith.
what i meant by listing all files manually in a flat file and then choosing a random image from it. should be listing all images files in a flat file through such mechanism and then choosing a random image from it.
The function of flat file here is as "cache". The existence of flat file will reduce time wasting while reading directory and generating seed. This process could be called automatic, since you don't write all filenames by yourself. You may also involve cron job to update the image file list in the file.

Here is an example code of how it works:




//Set up variables
$imgDir = "myImage/"; //relative to current file
$imgListFile = "imglist.txt";

function fileExtension ($givenFile) {
$arrayFile = explode(".",$givenFile);
$ext = count($arrayFile) - 1;
return $arrayFile[$ext];
}

function file_list($dir) {
global $imgListFile;
if (is_dir($dir)) {
$fd = @opendir($dir);
while (($part = @readdir($fd)) == true) {
if ($part != "." && $part != "..") {
if(is_file($dir.$part)) {
if (fileExtension($part) == "jpg" || fileExtension($part) == "jpeg" || fileExtension($part) == "gif" || fileExtension($part) == "png") {
$file_array[] = $part;
}
}
}
clearstatcache();
}
if ($fd == true) {
closedir($fd);
}
if (is_array($file_array)) {
natsort($file_array);
$fp = fopen($imgListFile,'wb');
if(is_writable($imgListFile) && flock($fp,LOCK_EX)) {
for($i=0;$i<count($file_array); $i++)
$content .= $file_array[$i].";";
$content = preg_replace("/;$/","\n",$content);
fwrite($fp,$content);
flock($fp,LOCK_UN);
fclose($fp);
}
return $file_array;
}
else {
return false;
}
}
else {
return false;
}
}

function imgFromFile ($pathToFile) {
$fp = fopen($pathToFile,'rb');
if(!$fp)
return 0;
$fileImg = fread($fp,filesize($pathToFile));
fclose($fp);
$file_array = explode(";",$fileImg);
return $file_array;
}

function chooseImg($method = 1) {
if($method == 1) { //use file_list
global $imgDir;
$fileList = file_list($imgDir);

}
else { //use imgFromFile
global $imgListFile;
$fileList = imgFromFile($imgListFile);
}
$totElement = count($fileList) - 1;
$chosen = rand(1,$totElement);
return $fileList[$chosen];
}

//Usage:

$img = chooseImg(1);
print "Image chosen from <b>listing image file</b>: ".$img;

print "<br />";

$img = chooseImg(2);
print "Image chosen from <b>$imgListFile</b>: ".$img;




This code does run. When I made it, I'd tested it with dummy data. This is a special one for you!! Wish this help.. :)