The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.
I have seen the one provided by freeola in the PHP essentials but I need one where the filenames of the images does not need to be 1,2,3 etc.
Anyone know of any decent ones?
Did my Freeola PHP Random Image demo help???
If you're still having problems don't be afraid to ask...
But if you are all sorted, it would be nice to know.
Search Freeola Chat
> I put
>
>
>
> into the code where I would have normally put the image source.
> When I say it doesn't work it is just that, no error messages,
> no little image icons in the corner, just white space.
>
> Does anyone know of a site that uses the Freeola code above so I
> can see what I should be getting.
>
> Thanks
Hi cakey1,
I've created a demo page using Freeola PHP script for you:
www.hmmm.ip3.co.uk/freeola_random
Hope this helps...
Search Freeola Chat
<?php
if ($dir = opendir("random_content")) {
// If random_content folder can be opened
$imgList = array();
while (false !== ($file = readdir($dir))) {
// While there are still files in the directory
if (preg_match('/.jpg$/', $file)) {
// If current file is a .jpg file
array_push($imgList, $file);
}
}
$key = array_rand($imgList, 1);
// Get a random key from the imgList array
$img = $imgList[$key];
// Do what you want with random image/content, eg:
echo('<img src="random_content/' . $img . '" alt="Random Image" />');
}
?>
You obviously have to change the "random_content" bit in the opendir() function to the actual folder you'll be getting the content from, and also alter the img src to the correct folder too.
You can change the preg_match() first parameter so it matches any filetype, or a range of filetypes.
.txt files =
preg_match('/.txt$/', $file)
.gif files =
preg_match('/.gif$/', $file)
.png files =
preg_match('/.png$/', $file)
etc.
Or, to match multiple file types (perhaps you want to match a range of image types), use code like the following:
preg_match('/(\.jpg | \.gif | \.png | \.bmp)$/', $file)
The preg_match() check isn't strictly necessary if you know only one type of file will be in the directory of random things.
If you want random content other than images, for example, a random message, you can use the same code and store each message in a text file, and just change the output, eg:
Instead of
echo('<img src="random_content/' . $img . '" alt="Random Image" />');
Use
echo('<p>' . $img . </p>);
You may want to change the names of the $imgList & $img variables for clarity too.
Perhaps easier to maintain than seperate text files though is to just hardcode random messages into the PHP, eg:
<?php
$msgList[0] = "Random Text 1";
$msgList[1] = "Random Text 2";
$msgList[2] = "Random Text 3";
$key = array_rand($msgList, 1);
$msg = $msgList[$key];
echo('<p>' . $msg . '</p>');
?>
Finally, if you want to obtain multiple random... things. The second parameter of array_rand() has always been 1 thus far. This specifies how many keys you want from the specified array. If it's set to 1 you get a single random key back. Otherwise you get an array of keys back.
So, rather than accessing your random item by something of the form:
$randomThing = $randomList[$key];
Now you do it by something like:
$randomThing1 = $randomList[$keys[0]];
$randomThing2 = $randomList[$keys[1]];
etc. up to however many elements you had.
A simpler way of doing this is with a for loop, eg.
for ($i=0; $i<sizeof($keys); $i++) {
// For each element of $keys
$random[$i] = $randomList[$keys[$i]];
}
Which puts each random element into an array $random for you to access as you like.
A final note about the array_rand() function... Make sure your random array contains greater than or equal to the number of random elements you are requesting otherwise array_rand() won't work!
Hopefully that will be useful to someone!
into the code where I would have normally put the image source.
When I say it doesn't work it is just that, no error messages, no little image icons in the corner, just white space.
Does anyone know of a site that uses the Freeola code above so I can see what I should be getting.
Thanks
> Try putting your PHP in proper PHP tags,
<?php code
> ;?>
. You should always avoid shorthand tags, it> doesn't save time anyway! The web servers are setup to parse
> code in full PHP tags as PHP
<? ?>
can just> be interpreted as XML or other language tags.
Nag nag nag.....
its people like you that take the fun out of coding. :p
<?php code ;?>
. You should always avoid shorthand tags, it doesn't save time anyway! The web servers are setup to parse code in full PHP tags as PHP <? ?>
can just be interpreted as XML or other language tags.
<?php
$imageDirectory='images';
if (substr($imageDirectory,-1)!='/') {
$imageDirectory.='/';
}
$images = array();
$cnt = -1;
$dir = opendir($imageDirectory);
while (false !== ($img = readdir($dir))) {
if (preg_match ('/^(.+)\.(jpg|gif|png|jpeg)$/i', $img)) {
$images[] = $img;
$cnt++;
}
}
closedir($dir);
echo '<img src="'.$imageDirectory. $images[mt_rand(0,$cnt)].'" alt="Random Image"/>';
?>
Save it as randomImage.php. Change the
$imageDirectory
variable with the path to your image directory. And where ever you want the random image to appear on your page, just do <?php include('randomImage.php') ?>
(ie you use it exactly as you would the freeola version)