Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-24-2010, 11:58 PM   PM User | #1
orbnexys
New to the CF scene

 
Join Date: Apr 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
orbnexys is an unknown quantity at this point
show database data one by one

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
orbnexys is offline   Reply With Quote
Old 04-26-2010, 01:38 AM   PM User | #2
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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

Last edited by DJCMBear; 04-26-2010 at 09:05 PM..
DJCMBear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:10 PM.


Advertisement
Log in to turn off these ads.