Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Get random image script.

Featured Replies

Hiya, I have just found this script on the net so that I can use it for displaying random images.

 

The script I have is only displaying one image multiple times but I want it to display images without repeating.

 

Any guidance would be good thankyou! :D

 

<?php
/*
By Matt Mullenweg > http://photomatt.net
Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php
Latest version always at:
http://photomatt.net/scripts/randomimage
*/// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder = '';

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); // Voila!
?>

Ok -

 

what images do you have in your DIR its looking in?

 

and to make it clear to me... is it one image you want to rotate on each page visit?

  • Author

Okay its a 'advert' section of my website. I'm telling it to call images from that directory but I dont want any images repeated when there called.

 

I want a total of 6 images showing by doing:

 

<li><img src="images/adverts/flash_den.jpg" alt="Advert" title="Advert for FlashDen"></li>
<li><img src="images/adverts/audio_jungle.jpg" alt="Advert" title="Advert for AudioJungle"></li>
<li><img src="images/adverts/theme_forest.jpg" alt="Advert" title="Advert for ThemeForest"></li>
<li><img src="images/adverts/graphic_river.jpg" alt="Advert" title="Advert for GraphicRiver"></li>
<li><img src="images/adverts/adverts.php" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php" alt="Advert" title="Advert"></li>

 

The images/adverts/adverts.php is the link to my random image script.

 

Also I currently have a total of 10 images in that directory but I will be getting more?

Edited by AdamConder

ive just tested the script and it worked ok?

when you say its displaying one image multiple times, how many times is it repeating it? and when you refresh the page is it always the same image that displays?

  • Author

If I changed the link to the php file on all of the list items.:

 

<li><img src="images/adverts/adverts.php" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php" alt="Advert" title="Advert"></li>

 

To something like that ^^. It tends to load one image and then repeated it 5 more times instead of loading different images that's why I'm wondering if there's anything I can write to stop repeating the file name etc?

  • Author

Hmm, I've been thinking. I think its doing that because I'm calling the file multiple times so its going to do the same operation? :S

 

Now would it do the same if I echoed out the list items with the image path being the http request?

 

I've got little experience with php but is this possible?

my way around it was to create a function out of it, and not to set the $rand, so that it got a new image each time

 

HTML:

<?php include("advert.php"); ?>
<li><img src="<?php getImage(); ?>" alt="Advert" title="Advert"></li>
<li><img src="<?php getImage(); ?>" alt="Advert" title="Advert"></li>
<li><img src="<?php getImage(); ?>" alt="Advert" title="Advert"></li>
<li><img src="<?php getImage(); ?>" alt="Advert" title="Advert"></li>
<li><img src="<?php getImage(); ?>" alt="Advert" title="Advert"></li>
</body>

 

new PHP:

<?php
/*
By Matt Mullenweg > http://photomatt.net
Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php
Latest version always at:
http://photomatt.net/scripts/randomimage
*/// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
function getImage()
{
$folder = '';

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

echo $folder.$files[mt_rand(0, $i)]; // Voila!
}
?>

  • Author

Got it working:

 

<h2 class="rightheader">Sponsor</h2>
<ul id="advert">
<li><img src="images/adverts/adverts.php?i=0" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php?i=1" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php?i=2" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php?i=3" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php?i=4" alt="Advert" title="Advert"></li>
<li><img src="images/adverts/adverts.php?i=5" alt="Advert" title="Advert"></li>
</ul>

 

<?php
// rotate images randomly but w/o dups on same page - format:
// <img src='rotate.php?i=0'> - rotate image #0 - use 'i=1'
// for second, etc
// (c) 2004 David Pankhurst - use freely, but please leave in my credit
$images=array( // list of files to rotate - add as needed
"advert_1.png",
"advert_2.png",
"advert_3.png",
"advert_4.png",
"advert_5.png",
"advert_6.png",
"audio_jungle.jpg",
"flash_den.jpg",
"graphic_river.jpg",
"theme_forest.jpg" );
$total=count($images);
$secondsFixed=10; // seconds to keep list the same
$seedValue=(int)(time()/$secondsFixed);
srand($seedValue);
for ($i=0;$i<$total;++$i) // shuffle list 'randomly'
{
$r=rand(0,$total-1);
$temp =$images[$i];
$images[$i]=$images[$r];
$images[$r]=$temp;
}
$index=(int)($_GET['i']); // image index passed in
$i=$index%$total; // make sure index always in bounds
$file=$images[$i];
header("Location: $file"); // and pass file reference back
?>

 

Thanks for help Anth!

great - theres probebly hundreds of ways out there :p

although i know that with my code you will still run into images displaying multiple times, as its just random selection

 

Glad you got it :)

  • Author

Now I'm wondering how I would get the links working because Ill need to do mysql query's then wont I?

 

I know how to grab text from databases but how do I do images? :S

in your mqsql database you could have something like

 

id, companyname, URL, imagename

 

with values of

 

digital winter, http://www.digitalwinter.co.uk, digitalwinter.jpg

 

then pull it from the database like so

 

$query = mysql_query("SELECT * FROM companys order by id asc LIMIT 6", $connection);

while ($row  = mysql_fetch_array($query)) {
echo "<li><a href=\"$row['URL']\" title=\"$row['companyname']\"><img src=\"$row['imagename']\" /></a></li>";
}

 

or something along those lines.....

 

this would take away the random feature though... and display each entry as entered in the database

  • Author

kk, got you!

 

Yea I was thinking it would take away the randomness!

 

I'm just trying to expand my php and mysql and greatly appreciate your help.

  • Author

This is what I've got but I'm getting a parse error? :S

 

<?php 
$connect = mysql_connect("localhost", "root", "password");
$query = mysql_query("SELECT * FROM advert order by id asc LIMIT 6", $connect);

while ($row  = mysql_fetch_array($query)) {
echo "<li><a href=\"$row['URL']\" title=\"$row['name']\"><img src=\"$row['imagename']\" /></a></li>";
}
?>

  • Author

Sure:

 

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\GamerZone\reviews.php on line 59

 

Line 59:

 

echo "<li><a href=\"$row['URL']\" title=\"$row['name']\"><img src=\"$row['imagename']\" /></a></li>";

ahh, its missing the wrap quotes, try this

 

echo "<li><a href=\"".$row['URL']."\" title=\"".$row['name']."\"><img src=\"".$row['imagename']."\" /></a></li>";

  • Author

Page is now loading but I'm getting:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\GamerZone\reviews.php on line 58

  • Author

Yes got it working as intended!!! :D

 

Had to do:

 

$query = mysql_query("SELECT * FROM advert order by RAND() asc LIMIT 6", $connect);

 

For the random listing :D, Thankyou so much for your help!

no problems :) i thought there might be some random way of showing results....

 

Learn something new everyday haha

  • Author

haha :D Now I'm setting myself the task of doing "pretty url's" you know any good links? :D

u mean pretty urls as in rewriting URLs? - im not too knowledgable on this area im afraid

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.