Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

Sequential Image Rotation

I need a script that changes images every time the page is refreshed , the images should change is sequence not in random , for I have an image with is cut into two parts on the  right side and one in the middle, when the page is reloaded the images should change in sequence.
  • 7
  • 6
2 Solutions
Roger BaklundCommented:
On what plattform will this be running, which scripting language do you want to use?
nicklb007Author Commented:
The platform is Linux , any scripting language would work , javascript , php or perl...
Roger BaklundCommented:
Please explain in more detail what you want to do.
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

nicklb007Author Commented:
I need a script that rotate images  in sequence every time the page loads , the script can be written in any language. for example the following JavaScript script will rotate images in sequence every certain time , I need the rotation to be on page reload not every 5 seconds as in this example

<script type="text/javascript" src="bannerRotator.js"></script>
<script language="javascript">
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {

// Banner 1

var myBanner1 = new BannerRotator();

myBanner1.add('', 'images/banner1.jpg');
myBanner1.add('', 'images/banner2.jpg');
myBanner1.add('', 'images/banner3.jpg');

myBanner1.timer = 5;// 5 secs between images

// Banner 2

var myBanner2 = new BannerRotator();

myBanner2.add('', 'images/banner_side1.jpg');
myBanner2.add('', 'images/banner_side2.jpg');
myBanner2.add('', 'images/banner_side3.jpg');

myBanner2.timer = 5;// 5 secs between images

// Set-up display

  myBanner1.bind('banner1');// match to ID of element.

  myBanner2.bind('banner2');// match to ID of element.
Roger BaklundCommented:
You have two sets of banners, "Banner1" and "Banner2". In the example there was three of each, was that a coincidence or are the banners related to each other, like pairs?

Do you want to rotate the banners per user or globaly? The difference is, if you rotate globaly, you will show each banner equally many times. If it rotates per user, the first banner will get more views, the second fewer (requires a reload) and the third even fewer, it requires that the user reloads the page twice.
nicklb007Author Commented:
The images are related to each other like page for example banner1 and side1 needs to be loaded at the same time , I prefer the rotation to be globally
Roger BaklundCommented:
ok. Then you need a single global counter. This is done with an integer. You can have a variable starting with value 1, incrementing by 1 for each page load until it is larger than the number of banners, then it resets to 1. Or you can have a counter which goes on for ever, and use a MOD operator to map any number to a banner.

You need to store this counter somewhere, is a mysql database an option?
nicklb007Author Commented:
sorry the mysql database is not an option for I don't have access to the control panel, on the other hand I have a script that worked on my test server but did not work on the main server . The script is php
            $max=3;//quantity of images

$fp=fopen ("img.txt", "r");
$n=fread ($fp, filesize ("img.txt"))+1;  
if ($n>$max)
fclose ($fp);

$fp=fopen ("img.txt", "w");
fwrite ($fp, $n);
fclose ($fp);

print '<img src="pic'.$n.'.gif"  height="180" width="446"  border="0" />';

The script created img.txt  on my test server, and the sequence is ok when I moved the script to my main server, img.txt was not created instead the first image is displayed . the server has PHP Version 4.4.1 installed with the following info
Server API       Apache
Virtual Directory Support       disabled
Configuration File (php.ini) Path       /etc/apache/php.ini
PHP API       20020918
PHP Extension       20020429
Zend Extension       20050606
Debug Build       no
Zend Memory Manager       enabled
Thread Safety       disabled
Registered PHP Streams       php, http, ftp, https, ftps, compress.bzip2, compress.zlib

Zend logo This program makes use of the Zend Scripting Language Engine:
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

PHP Credits
PHP Core
Directive      Local Value      Master Value
allow_call_time_pass_reference      Off      Off
allow_url_fopen      On      On
always_populate_raw_post_data      Off      Off
arg_separator.input      &      &
arg_separator.output      &      &
asp_tags      On      On
auto_append_file      no value      no value
auto_prepend_file      no value      no value
browscap      no value      no value
default_charset      no value      no value
default_mimetype      text/html      text/html
define_syslog_variables      Off      Off
disable_classes      no value      no value
disable_functions      no value      no value
display_errors      Off      Off
display_startup_errors      Off      Off
doc_root      no value      no value
docref_ext      no value      no value
docref_root      no value      no value
enable_dl      On      On
error_append_string      no value      no value
error_log      no value      no value
error_prepend_string      no value      no value
error_reporting      2047      2047
expose_php      On      On
extension_dir      /usr/lib/php/extensions/      /usr/lib/php/extensions/
file_uploads      On      On
gpc_order      GPC      GPC      #FFFFFF      #FFFFFF
highlight.comment      #FF8000      #FF8000
highlight.default      #0000BB      #0000BB
highlight.html      #000000      #000000
highlight.keyword      #007700      #007700
highlight.string      #DD0000      #DD0000
html_errors      On      On
ignore_repeated_errors      Off      Off
ignore_repeated_source      Off      Off
ignore_user_abort      Off      Off
implicit_flush      Off      Off
include_path      .:/usr/lib/php      .:/usr/lib/php
log_errors      On      On
log_errors_max_len      1024      1024
magic_quotes_gpc      Off      Off
magic_quotes_runtime      Off      Off
magic_quotes_sybase      Off      Off
max_execution_time      30      30
max_input_time      60      60
memory_limit      16M      16M
open_basedir      no value      no value
output_buffering      4096      4096
output_handler      no value      no value
post_max_size      8M      8M
precision      14      14
register_argc_argv      Off      Off
register_globals      Off      Off
report_memleaks      On      On
safe_mode      Off      Off
safe_mode_exec_dir      no value      no value
safe_mode_gid      Off      Off
safe_mode_include_dir      no value      no value
sendmail_from      no value      no value
sendmail_path      /usr/sbin/sendmail -t -i       /usr/sbin/sendmail -t -i
serialize_precision      100      100
short_open_tag      On      On
SMTP      localhost      localhost
smtp_port      25      25
sql.safe_mode      Off      Off
track_errors      Off      Off
unserialize_callback_func      no value      no value
upload_max_filesize      2M      2M
upload_tmp_dir      no value      no value
user_dir      no value      no value
variables_order      GPCS      GPCS
xmlrpc_error_number      0      0
xmlrpc_errors      Off      Off
y2k_compliance      On      On
Michel PlungjanIT ExpertCommented:
Cookie perhaps?
Roger BaklundCommented:
Insert error checking:
$max = 3; //quantity of images
$fp = @fopen ("img.txt", "r");
if($fp) {
  $n = fread($fp, filesize("img.txt"))+1;  
  if($n > $max) $n = 1;
} else $n = 1;
$fp = @fopen("img.txt", "w");
if($fp) {
  fwrite($fp, $n);
} else echo '(Could not write banner counter!)';
echo '<img src="pic'.$n.'.gif"  height="180" width="446"  border="0" />';

Open in new window

nicklb007Author Commented:
I did insert the error checking and got (Could not write banner counter!), with the first image in sequence displayed .  
Roger BaklundCommented:
The script is not allowed to create a file, but it might be allowed to modify it. Create a file named "img.txt" containing a single character 0 (zero). Upload it to the server. Then try again.
nicklb007Author Commented:
Thank you the script worked I created the file uploaded to server and changed the permission of img.txt  to 777

nicklb007Author Commented:
question answered
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to and use offer code ‘EXPERTS’ to get 10% off your first purchase.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now