473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

random rotating image banner from folder source

Hi, I'm still kinda new to PHP so I could do with some advice. I'm using the code below to select and show a random image from a folder of images.

rotate.php
[PHP]<?php

// 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!
?>[/PHP]


The file is called as a standard image link: <img src="rotate.php">
What I'd like to do is have the image change after time limit and/or have a HTML link that will force an image change. I've seen similar stuff, but I like the idea that I can just add a new image to the folder and it will automatically be used without any code change.

The problems are as follows:
I can't add to the HTML source, with the exception creating the link and image source (long story, I'm using it on a blog!). I can upload more source files etc. if needs be.

Anyone who can suggest a solution, I'd greatly appreciate it.

Cheers!
Mar 21 '07 #1
1 2461
code green
1,726 Expert 1GB
There are a couple of ways of doing this. I store the file address of my rotating banners in a mysql database and select them randomly with a preferential weight..
But if you want to stick to files only then load the file address within your loop into an array then use array_rand();
Mar 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ian Hubling | last post by:
I'm trying to complete a rotating banner ad within a page I have. The rotating add has four images that rotate in three-second increments. I've got the images to rotate ok - but now I want to go...
1
by: Sandy Bremmer | last post by:
I have seen many Javascripts that rotate images with each load or refresh of the page but so far all I've found require hard coding the image filename into the script. Does anyone know of a script...
1
by: Grunt | last post by:
Hi, I have been trying to put together a rotating banner. the code works but I am having a problem with the caching of the banner images. no matter what I try the page is constantly reloading the...
2
by: sgMuser | last post by:
Hi, I am not a good developer of Javascript codes. Needs this help to make some modification to this famous free javascript from Anarchos. i am using this in one of my webpage. What it does is,...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
10
by: Joey_Stacks | last post by:
Does anyone know of a scipt that will rotate random div layers on page refresh? I have a primary content area front and center on my site homepage, and I'd like to rotate various chunks of html...
1
by: kennykenn | last post by:
Im having trouble startin to develop a rotating image banner, I have coded part of it (only pulling image from a db). My aim is to print an image to part of my web page, allow it to screen for x...
5
by: rodmc | last post by:
Hi, I have a set of CGI scripts set up and in one page (which is stored in an HTML file then printed via a python CGI) there is an image. However the image never displays, can anyone recommend a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.