This is asking a question and not a tutorial on a php snippet so this should have been put in the php forum not the snippet section.
Anyway I have done this for you, and as php code only run before page loads you have to use javascript to allow the image to change but all the function is doing is created a javascript array from the php array and then outputting the full javascript code to allow the images to show and change.
- advert.php
PHP Code:
<?php # Advert Creator - advert.php
function Adverts($phpArr=0,$time=1000){
if($phpArr){ $jsArr = "";
foreach($phpArr As $key => $Arr){ $jsArr .= " ['{$Arr}'],\n"; }
$jsArr = "var img = [\n".substr(trim($jsArr),0,-1)."\n];\n";
$data = "/*\n * (c)2010 CBSystems, All Rights Reserved.\n";
$data .= " * This script was created for 'orbnexys'.\n * Created on: ";
$data .= "April 26 2010, 1:23 AM.\n */\n";
$data .= "var which=0\nnew Array()\n{$jsArr}\n";
$data .= "var preloadedimages=new Array()\nfor (i=0;i<img.length;i++){\n";
$data .= " preloadedimages[i]=new Image()\n preloadedimages[i].src=img[i]\n}\n";
$data .= "\nfunction Run(){\n if (which<img.length-1){\n which++\n";
$data .= " document.images.photoslider.src=img[which];\n";
$data .= " document.images.photoslider.alt=img[which];\n";
$data .= " }else{\n which = 0;\n document.images.photoslider.src=img[0];\n";
$data .= " document.images.photoslider.alt=img[0];\n }\n setTimeout(\"Run()\",{$time});\n";
$data .= "}\n\nvar ad = '<img src=\"'+img[0]+'\" alt=\"'+img[0]+'\" ";
$data .= "name=\"photoslider\" style=\"filter:revealTrans(duration=2,transition=23)\">';\n";
$data .= "document.write(ad);\n\nwindow.load=setTimeout(\"Run()\",{$time});";
return $data; }
}
$Arr = array('image1.png','image2.png','image3.png','image4.png','image5.png');
// This array can be change to call from a mysql database with mysql_fetch_array.
print Adverts($Arr,4000);
?>
- page.html
Code:
<!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" lang="en" xml:lang="en">
<head>
<title>Try Adverts</title>
</head>
<body>
<script src="advert.php" type="text/javascript"></script>
</body>
</html>
__________________
Official
BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script
here.
Go Pledge for
Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P