Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Thanked 0 Times in 0 Posts

    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

  2. #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    United Kindom
    Thanked 136 Times in 136 Posts
    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){
    $phpArr){ $jsArr "";
    $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});";
    $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>
    Last edited by DJCMBear; 04-26-2010 at 10:05 PM.
    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts