View Full Version : show database data one by one

04-24-2010, 11:58 PM
Hi, I have multiple images in my database. I want to extract all of them on page load and show 1 ad for 4 second and then show another ad for same time period.
If I will run a while loop then all images will appear on the page at the same time but I want 1 image to show for some time and another image to take first image position for same time and then so on. Please Help.

BTW, I am working in PHP

04-26-2010, 01:38 AM
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 # 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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<title>Try Adverts</title>
<script src="advert.php" type="text/javascript"></script>