Page 1 of 1

PHP image randomiser help

Posted: Sun Jan 16, 2011 6:23 pm
by Sneblot
I am attempting to do is random image rotation on the index page, I have followed this sitepoint tutorial on how to code a php image rotation and would like to make sure that the page calls on the php file properly. I have created a separate php file below is the code:

Code: Select all

<?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 
	"images/paulinewoolley/bjarg.jpg",
	"images/paulinewoolley/ion.jpg",
	"images/paulinewoolley/foss.jpg",
	"images/paulinewoolley/altitude1.jpg",
	"images/paulinewoolley/altitude5.jpg",
	"images/paulinewoolley/altitude6.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 
?>
Here is the code that would be placed on the html file

Code: Select all

<img src='canstudios.org/rotate.php?i=0'>image #1 
<img src='canstudios.org/rotate.php?i=1'>image #2 
<img src='canstudios.org/rotate.php?i=2'>image #3
For some reason the image randomiser will not work. I'm not sure if I'm calling on the php correctly.

Here is the html code from the index.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" type="text/css" href="css/style.css" /> 
<title>Can Studio</title> 
</head> 
<center> 
<body> 
<div class="wrapper"> 
<div class="header"> 
<img src="images/logo.jpg" align="left" border="0" /> 
</div> 
<ul class="navigation"> 
<li><a href="contact.html">Contact</a></li> 
<li class="sub"><a href="#">Artists</a> 
	<ul class="sub"> 
    	<li><a href="kit_anderson.html">Kit Anderson</a></li> 
        <li><a href="liza_aspell.html">Liza Aspell</a></li> 
        <li><a href="karen_frazer.html">Karen Fraser</a></li> 
        <li><a href="billie_ireland.html">Billie Ireland</a></li> 
        <li><a href="sarah_jane_terry.html">Sarah Jane Terry</a></li> 
        <li><a href="marek_tobolewski.html">Marek Tobolewski</a></li> 
        <li><a href="inge_tong.html">Inge Tong</a></li> 
        <li><a href="pauline_woolley.html">Pauline Woolley</a></li> 
        <li><a href="terry_duffy.html">Terry Duffy</a></li> 
    </ul> 
</li> 
<li><a href="events.html">Events</a></li> 
<li><a href="news.html">News</a></li> 
<li><a href="index.html">Home</a></li> 
</ul> 
<div class="index-spacer"></div> 
<?php include("rotate.php"); ?> 
<img src='canstudios.org/rotate.php?i=0'>image #1
<img src='canstudios.org/rotate.php?i=1'>image #2
<img src='canstudios.org/rotate.php?i=2'>image #3
<img src='canstudios.org/rotate.php?i=3'>image #4
<img src='canstudios.org/rotate.php?i=4'>image #5
<img src='canstudios.org/rotate.php?i=5'>image #6
<div class="information"><p class:"information">Founded in 1987, Can Studios are one of the longest running studio groups in Nottingham today. Members of the group have instigated innovative and enduring artist-led activities which have included collaborations, warehouse exhibitions, open studios, offsite projects, art auctions, residencies, gallery collaborations and installations in alternative urban venues. These endeavours have stimulated the growth of contemporary art in Nottingham over the last 20 years. Can Studios now occupies a new location in Sneinton, near to the city centre. Here the studio members continue their committed involvement in individual projects, and in developing ambitious new concepts for collaborative possibilities.</p></div></div> 
</body> 
</center> 
</html>

Re: PHP image randomiser help

Posted: Sun Jan 16, 2011 9:06 pm
by Jonah Bron
I haven't looked at the PHP code, but you do need to put "http://" at the beginning of the image URL.

Code: Select all

<img src="http://canstudios.org/rotate.php?i=0">

Re: PHP image randomiser help

Posted: Mon Jan 17, 2011 5:59 am
by Sneblot
Thanks for the advice, I will implement that and see how it works?